2013-02-24 56 views
0

我有一个对象Item可以是ItemType之一。该ItemType有一个attribute这是truefalse使用相关模型属性返回对象

Class Item 
    belongs_to :item_type 
end 

Class ItemType 
    has_many :items 
end 

目前,我只是抓住以通常的方式最后x对象:

latest_items = Item.last(x) 

现在,我需要返回的xItems一个列表,其中ItemTypeattribute设为true,像这样(伪):

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

这是R中很容易实现苦恼的?现在看来似乎应该是简单的,但我四处摸索,无法找到解决方案

回答

1

您可以通过使用

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

完美工作很容易做到这一点 - 非常感谢 – 2013-02-24 20:53:55

+0

不客气: ) – 2013-02-24 20:54:32