I’ve created a model for an existing table using the following generator command:
script/generate model Group
The table in question have a different name, so I’ve changed the model to account for it.
set_table_name 'demandegroupe'
Then I’ve fired up the console to look if everything was working.
>> Group.all
[#<Group login: "XXXXXX", ...>, ...]
But, to my surprise, using this model in a view throws out weird errors. I returned to the console to make sure I wasn’t hallucinating and here’s what happened:
>> Group.first
#<Group login: "XXXXXX", ...>
>> Group.first.login
NoMethodError: undefined method `generated_methods' for 50:Fixnum
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/attribute_methods.rb:247:in `method_missing'
from (irb):2
After that the first expression is not working anymore:
>> Group.first
NoMethodError: undefined method `column_names' for 50:Fixnum
All columns are either varchar or int, where’s that 50:Fixnum type is coming from?
Thanks
Finally, I made a huge mistake, or more precisely overlooked an important detail! It’s a simple column name clash, I had review them for potential issues, but missed one buried in the 26 others. That malicious column was named
classand once AR generated it’s magic code after accessing any column it just replaced the vital class method without throwing any error.I expected few name clashed as the column names seemed all written in French, so didn’t took enough time and must have read “classe” or something like that. Beware the mighty name clash!
Thanks to ehsanul and dmajkic for making me review those column names a second time.