我的模型设置类似于:
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)
但是,问题在于,如果最近在同一本书上发生了两个动作,则将检索这两个动作。我只想检索最新的。我如何实现这一目标?