2013-02-24 3 views
0

ItemType 중 하나 일 수있는 Item 개체가 있습니다. ItemTypeattribute이며 true 또는 false입니다. 순간 관련 모델 특성을 사용하여 개체를 반환하는 중

Class Item 
    belongs_to :item_type 
end 

Class ItemType 
    has_many :items 
end 

, 난 그냥 일반적인 방법으로 마지막 x 물체를 잡는 오전 :

latest_items = Item.last(x) 

을 지금, 나는이 ItemType가에 attribute 세트가 xItems의 목록을 반환해야 이 (의사) 같은 true 뭔가 :

latest_items = Item.last(x).where(Item.item_type.attribute = "true") 

는 R이 쉽게 달성 에일? 똑바로 앞으로해야 것 같다,하지만 난 솔루션

답변

1

을 찾을 수없는, 주위 scrabbling있어 당신은

latest_items = Item.joins(:item_type).where("item_type.attribute = ?", true).limit(x).order('id desc') 
+0

가 완벽하게 작동 사용하여 쉽게 할 수 - 매우 –

+0

을 덕분에 당신은 환영합니다 :) –

관련 문제