Activerecord Rails 3-示波器
我的模型设置与此类似:Activerecord Rails 3-示波器,activerecord,ruby-on-rails-3.2,Activerecord,Ruby On Rails 3.2,我的模型设置与此类似: 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 defa
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
但是,问题是,如果同一本书上最近发生了两个操作,则将检索这两个操作。我只想检索最近的一个。如何实现这一点?我发现我想要的是团体选项。所以我可以这样做:
Action.limit(5)
Action.group(:object_id).group(:object_type).limit(5)