Confusedly about the Rails has_and_belongs_to_many Association

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
+3
1

, , . , , . , . . - ^ _ ^

class User < ActiveRecord::Base
  has_many :skills_users
  has_many :skills, through: :skills_users

class SkillsUser < ActiveRecord::Base
  belongs_to :user
  belongs_to :skill

class Skill < ActiveRecord::Base
  has_many :skills_users
  has_many :users, through: :skills_users
0

All Articles