I have two models, elements, and categories that are many-to-many, using the has_and_belongs_to_many association.
In my models I have
class Item < ActiveRecord::Base
has_and_belongs_to_many :categories
end
and
class Category < ActiveRecord::Base
has_and_belongs_to_many :items
end
I created a connection table "categories_items":
create_table "categories_items", :id => false, :force => true do |t|
t.integer "category_id"
t.integer "item_id"
end
I am not getting any errors, but I'm just a little confused about what association allows. Right now, if I have the @category category, I can find all the elements in it by doing
@category.items
I suggested that I can find the categories associated with this Item @item by doing
@item.categories
However, I get the error ActiveModel :: MissingAttributeError: missing attribute: category
I donβt understand how the has_and_belongs_to_many association works, or am I missing something in my code? Thank!
Edit - Additional Information:
, , /. :
@item = Item.new
... add attributes ...
@item.save
@category = Category.new
... add attributes ...
@category.save
@category.items << @item
@item.categories << @category