2013-05-24 2 views
0

나는 다음과 같은 자원이 있습니다 - 레스토랑 - 카테고리 - 항목 - 검사 항목을레일 필터링 자원 하나

관계 : 나는 모든 필터링해야

class Restaurant < ActiveRecord::Base 
    has_many :items 
    has_many :categories 
    has_many :check_items 

class Category < ActiveRecord::Base 
    belongs_to :restaurant 
    has_many :items 

class Item < ActiveRecord::Base 
    belongs_to :restaurant 
    belongs_to :category 

class CheckItem < ActiveRecord::Base 
    belongs_to :item 

레스토랑의 check_items where category.uuid = '123123'

그래서 나는 @restaurant.check_items을 가지고 있습니다. 어떻게 기본적으로이 SQL 쿼리를 구현하기 위해 함께이 참여 않습니다

SELECT * from checkitem 
INNER 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; 

나는 범위와 시도했다 :

#already have my restaurant resource here with id 1 
@restaurant.check_items.by_item_category params[:category_uuid] 

그리고 내 모델

나는 것 :

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 이 작동하지 않는 것 같습니다

답변

0

이것은 누군가가 int 경우 작동하는 것으로 나타났습니다 유일한 방법 인 것 같습니다 들렀다.

CheckItem.joins (: item => {: category => : restaurant}) 여기서 '(category.uuid =? and restaurant.id =?', 123123, 1)