Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. class Category < ActiveRecord::Base
  2. has_many :item_types
  3. has_many :items, :through => :item_types, :source => :category
  4. end
  5.  
  6. class ItemType < ActiveRecord::Base
  7. belongs_to :category
  8. has_many :items
  9. end
  10.  
  11. class Item
  12. belongs_to :item_type
  13. end
  14.  
  15. Category.joins(:item_types,:items).where("category.id=?",1)
  16.  
  17. Category.joins(:item_types, :items).where(:item_type => {:category_id => 1})
  18.  
  19. # app/models/category.rb
  20. class Category < ActiveRecord::Base
  21. has_many :item_types
  22. has_many :items, :through => :item_types
  23. end
  24.  
  25. # app/models/item_type.rb
  26. class ItemType < ActiveRecord::Base
  27. belongs_to :category
  28. belongs_to :item
  29. end
  30.  
  31. # app/models/item.rb
  32. class Item < ActiveRecord::Base
  33. has_many :item_types
  34. has_many :categories, :through => :item_types
  35. end
  36.  
  37. # app/controllers/categories_controller.rb
  38. def show
  39. @category = Category.find(params[:id])
  40. @category_items = @category.items
  41. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement