I have the following resources:- restaurant- category- item- check item
Relationship:
class Restaurant < ActiveRecord::Base has_many :items has_many :categories has_many :check_itemsclass Category < ActiveRecord::Base belongs_to :restaurant has_many :itemsclass Item < ActiveRecord::Base belongs_to :restaurant belongs_to :categoryclass CheckItem < ActiveRecord::Base belongs_to :item
I need to filter all the check_items of a restaurant where category.uuid = '123123'
so I have my @restaurant.check_items
. How do I join these together to basically implement this sql query:
SELECT * from checkitemINNER JOIN item ON(checkitem.item_id = item.id)INNER JOIN category ON(category.id = item.category_id)WHERE category.restaurant_id = 1 AND category.uuid = '123123'LIMIT 20;
I've tried with scope:
#already have my restaurant resource here with id 1@restaurant.check_items.by_item_category params[:category_uuid]
And in my models I would have:
class CheckItem < ActiveRecord::Base ... scope :by_item_category, -> value { joins(:item).by_category value }class Item < ActiveRecord::Base ... scope :by_category, -> value { joins(:category).where('%s.uuid = ?' % Category.table_name, value)}
Buut this doesn't seem to work