2012-11-06 27 views
0

我已经款设立与此类似:的Rails 3 - 斯科普斯

class Book < ActiveRecord::Base 
    has_many :histories, as: :object 
end 

class Magazine < ActiveRecord::Base 
    has_many :histories, as: :object 
end 

class Action < ActiveRecord::Base 
    belongs_to :object, polymorphic: true 

    default_scope order(:done_at) 

    # a history contains an action and the time that action 
    # occurred on the object it belongs to 
end 

现在,我想对所有对象发生的5个最新的操作的列表。所以,我可以这样做:

Action.limit(5) 

然而,问题是,如果两个行动近日对同一本书发生后,两个动作将被检索。我只想检索最新的一个。我如何实现这一目标?

回答

0

找出我想要的是群组选项。所以我可以这样做:

Action.group(:object_id).group(:object_type).limit(5)