views:

30

answers:

2

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

A: 

Are you using Mysql? Group is a reserved sql keyword. It looks like Rails 2.3.5+ handles this for mysql, but it may be causing the problem you're seeing on other databases or other versions of Rails.

Winfield
The column name is different, so MySQL (hopefully) will never know about the model class name.
Nicolas Buduroi
A: 

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 class and 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.

Nicolas Buduroi
"You can accept your own answer in 2 days" Well, that's a weird rule!
Nicolas Buduroi