I have a column named _SOMETHING as part of an object created with rails g scaffold, say: rails g scaffold Person _SOMETHING:string
In the create method, when doing #person.save, an error pops up saying that it doesn't find method something(no underscore and lowercases).
Why is it looking for a method with that name?
I patched it by creating
def something
true
end
in my Person model. I am sure that's not the correct way to solve this.
Using Ruby 2.2.2, Rails 4.2.1
Thanks in advance
Turns out the model had a validation where I misspelled the column name, failing each time. Changed the name to _SOMETHING and it worked.
Thank you all for your attention to this silly issue
Related
When I try to generate a model using the next sentence:
rails g model SupplyRequestProfile name:string{50} active:boolean
It fails, and shows the next single line
No value provided for option '--orm'
But, when I use, skipping the fields and types:
rails g model SupplyRequestProfile
It does the job and all is normal.
I have looked if I use a reserved word of rails for my propertys in http://reservedwords.herokuapp.com/ but there is no one that im using.
The problem is not the config/application.rb, all is ok there.
So, I dont know what could be happen here, but it is like some of my property names or types are wrong or causing some strange behavior on rails generation.
What could be wrong?
Simply putting the fields names with their types inside double quotes will work:
rails generate model SupplyRequestProfile "name:string{50}" "active:boolean"
O.k. this is driving me crazy - if anyone could help that would be great.
I have a simple table "threads" with an auto id and a "name" field with one record (id = 1, name = "space").
I have a model named "Thread.rb".
class Thread < ActiveRecord::Base
end
I have a controller that calls:
#thread = Thread.find_by_id(1)
But when loading a page I get the following error in the controller:
undefined method `find_by_id' for Thread:Class
I've used find_by_sql numerous times in the same project with no problem, but when using the simple activerecord accessors rails errors out on "find", "find_by_id" etc.
I'm on rails 4.0.2
I'm pretty sure Thread is a reserved word in Rails, I recall having the same issue. Renaming your class should solve it. I was able to find this community authored site with a list of reserved Rails words.
rails has the autoloading mechanism by hooking into the const_missing and the combination of some convention on constant naming and autoload_paths.
If you refer to a constant that is already predefined, either in the ruby standard library or the rails, then it will refer that constant to the already loaded one.
Thread is part of ruby standard library, which is why when you say Thread.find_by_id(1), it is referring to that instead of your model.
Refer to Thread and Module#const_missing for more info.
I am trying to access all records of a table that has an underscore in its name.
For example if I have table in my schema that is called trips I can do Trip.all in rails console. But what do I do if my table name contains an underscore (e.g. users_foods)
I tried the following options:
Users_food.all
User_food.all
User_foods.all
etc.
All of the above did not work, any suggestions?
Figured it out
One can access the data with UserFood
For a table named users_foods, ActiveModel should provide you with a corresponding Rails model UsersFood, to fit Ruby/Rails object naming convention. Try that.
Class names Users_food or User_food, etc. do not conform to Ruby convention.
I had the same problem when creating bonus_histories table. And didn't work any of the answers until I found out why.
I have made only rails g migration BonusHistory
and it was the problem. My rails console didn't find BonusHistory at all because I had no model.
So I had to first rollback the migration rake db:rollback STEP=1, then deleted the migration file and finally made
rails g model BonusHistory
and after migrating that table, when I enter rails console, I can successfully ask for BonusHistory.count
I have a model called MyArticle. When I try to use the command
rails generate migration AddtestToMyArticle test:string
the migration file contains only empty up/down methods. Having done this previously on a single word model name, it worked just fine and the migration up/down methods had the appropriate code.
I tried "AddtestToMy_Article" but that didn't work either. What do I need to do to work with my compound model name and the generate migration command?
You need to use
rails generate migration AddColumnToMyArticle test:string
When using 'AddColumn' you will have the appropriate code in your migration.
I just looked back over this, and while my answer is correct, it's better to have a more descriptive migration name. The user below who noticed the capitalization is right in that if you don't have each new word capitalized, Rails won't pick up on what exactly you're trying to do. So, in your question you have AddtestTo... but it should be AddTestTo....
It seems to work if you use underscores rather than CamelCase
rails g migration add_test_to_my_article test:string
Hope this helps
rails generate migration AddNewFieldToMyArticle new_field:string
On my blog I'm have posts that belong to a series. I've tried to scaffold series but there are some problems with routes.
The pluralization engine doesn't get it right so I had to manually change Sery, #series, and #sery which is not a big deal.
The routing seems to be ok with resources :series. But then when I try to create a series the form_for helper complains about the route.
And then when I create it with console it works but rails is still complaining about routes.
Please create a simple app and see what the problem is.
rails new test_series_app
And then run the scaffold generator:
rails g scaffold series name:string
And see how the routes are getting mixed up and help me out please!
For the record, I put the singularize code into the scaffold generator (yes, my one contribution to Rails). All it does is check if record_name == record_name.pluralize. If it does and you haven't passed in --force-plural, it calls record_name = record_name.singularize.
In this case "series".pluralize is the same as "series".singularize so I assume it won't do anything.
So if you're having problems w/ it, you need to write an inflector for the word.
(I wrote it after Jeremy Kemper's 2008 RailsConf keynote in which he accidentally passed in a plural model name causing himself all sorts of grief in the middle of his talk.)