Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Category < ActiveRecord::Base
- has_many :item_types
- has_many :items, :through => :item_types, :source => :category
- end
- class ItemType < ActiveRecord::Base
- belongs_to :category
- has_many :items
- end
- class Item
- belongs_to :item_type
- end
- Category.joins(:item_types,:items).where("category.id=?",1)
- Category.joins(:item_types, :items).where(:item_type => {:category_id => 1})
- # app/models/category.rb
- class Category < ActiveRecord::Base
- has_many :item_types
- has_many :items, :through => :item_types
- end
- # app/models/item_type.rb
- class ItemType < ActiveRecord::Base
- belongs_to :category
- belongs_to :item
- end
- # app/models/item.rb
- class Item < ActiveRecord::Base
- has_many :item_types
- has_many :categories, :through => :item_types
- end
- # app/controllers/categories_controller.rb
- def show
- @category = Category.find(params[:id])
- @category_items = @category.items
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement