given the following schema
class User
has_many :page_views
#[id]
end
class Page
has_many :page_views
#[id]
end
class PageView
belongs_to :user
belongs_to :page
#[id, created_at, updated_at]
end
How can i query for a given page, all users most recent page views in order by created_at date. One row per user, showing only their most recent view.
You want the PageViews with the maximum created_at for each user_id for a particular page_id. This is a representation of this query in SQL:
In Rails 3, you can write this like so: