Rails how to make tags in Product model by categories -


i have models product(title, description, category_id) , category(title, description).

class product < activerecord::base   belongs_to :category  class category < activerecord::base   has_many :products 

now product can have 1 category , it's working perfectly, want make, product can have 1 more categories tags, can't understand how..

you need has_and_belongs_to_many association documented here

edit: ok asked quick example

class product < activerecord::base   has_and_belongs_to_many :categories  class category < activerecord::base   has_and_belongs_to_many :categories 

migrations

create_table :categories |t|   t.string :title   ...  create_table :products |t|   t.string :title   ...  create_table :categories_products, id: false |t|   t.belongs_to :category, index: true   t.belongs_to :product, index: true end 

you can access products categories

product.find(1).categories 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -