Topic: Total n00b: association issues

Hi there,
I'm working on my first real RoR app. As I'm trying to model my db tables, I'm running into some problems. Here's the rundown:

There are two tables: items and categories.

From item.rb:

class Item < ActiveRecord::Base
  belongs_to    :category
end

From category.rb:
class Category < ActiveRecord::Base
  has_many  :items
end

Now, I want to be sure I'm doing this right, so I run some tests in irb. Here's what happens during my session;

>> a_cat = Category.new
=> #<Category:0x23b4220 @attributes={"name"=>nil, "parent_id"=>nil}, @new_record=true>
>> a_cat.name = "Dairy"
=> "Dairy"
>> a_cat.save
=> true
>> an_item = Item.new
=> #<Item:0x23a1e40 @attributes={"name"=>nil, "categories_id"=>nil, "label_id"=>nil}, @new_record=true>
>> an_item.name = "Milk"
=> "Milk"
>> a_cat.item << an_item
NoMethodError: undefined method `item' for #<Category:0x23b4220>
        from /Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/powerpc/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1792:in `method_missing'
        from (irb):30

By my lights, this is a pretty straightforward one-to-many relationship, but the code doesn't see the item method as part of the category. I'm sure this is something stupid simple, but I'm not seeing it. Any help would be appreciated!

Thanks,
Aaron

Re: Total n00b: association issues

category has_many items so you need to use the plural form.

a_cat.items << an_item

Railscasts - Free Ruby on Rails Screencasts

Re: Total n00b: association issues

Thanks for the quick reply!

Here's what happens:

>> a_cat.items << an_item
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'items.category_id' in 'where clause': SELECT * FROM items WHERE (items.category_id = 3)
        from /Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/powerpc/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract_adapter.rb:120:in `log'
        from /Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/powerpc/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/mysql_adapter.rb:184:in `execute'
        from /Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/powerpc/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/mysql_adapter.rb:336:in `select'
        from /Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/powerpc/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/mysql_adapter.rb:175:in `select_all'
        from /Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/powerpc/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:390:in `find_by_sql'
        from /Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/powerpc/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:924:in `find_every'
        from /Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/powerpc/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:381:in `find'
        from /Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/powerpc/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/associations/has_many_association.rb:83:in `find'
        from /Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/powerpc/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/associations/has_many_association.rb:111:in `find_target'
        from /Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/powerpc/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/associations/association_proxy.rb:116:in `load_target'
        from /Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/powerpc/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/associations/association_collection.rb:20:in `<<'
        from (irb):16

It says I'm missing item.category_id. Is that what it ought to be, since I'm actually using categories_id?

Cheers,
Aaron.

Re: Total n00b: association issues

Indeed, switch categories_id as a field name to category_id, it works great. Thanks for your help!

Aaron