Between category and product must be association (connection) so You must create special table for that.
If You want use has_and_belongs_to_many:
in models/product.rb:
...
has_and_belongs_to_many :categories, :join_table => :categories_products
...
in models/category.rb:
...
has_and_belongs_to_many :products, :join_table => :categories_products
...
in your migration file create_categories_products_join.rb
...
create_table :categories_products, :id => false do |t|
t.references :product
t.references :category
end
add_index :categories_products, [:product_id, :category_id]
add_index :categories_products, [:category_id, :product_id]
...
and remove categorization model rails d model categorization
If You want use has_many :through
in models/product.rb:
...
has_many :categorizations
has_many :categories, :through => :categorizations
...
in models/category.rb:
...
has_many :categorizations
has_many :products, :through => :categorizations
...
in models/categorization.rb
...
belongs_to :product
belongs_to :category
...
in migration file create_categorizations
...
create_table :categorizations do |t|
t.references :product
t.references :category
#...
t.timestamps
end
add_index :categorizations, :category_id
add_index :categorizations, :product_id
...
migration create_categories_products_join You can remove rails d migration create_categories_products_join