Ruby on Rails

Many To Many Relations

products
id int
name string
categories
id int
name string
categories_products
category_id int
product_id int
class Category < ActiveRecord::Base
  has_and_belongs_to_many :products
end

class Product < ActiveRecord::Base
  has_and_belongs_to_many :categories
end

#You can now use

acategory.products #get all products with acategory
aproduct.categories #yes, it pluralizes category correctly

José M. Vidal .

7 of 16