Skip to content

Instantly share code, notes, and snippets.

@mokolabs
Created April 20, 2011 17:22
Show Gist options
  • Save mokolabs/932037 to your computer and use it in GitHub Desktop.
Save mokolabs/932037 to your computer and use it in GitHub Desktop.
# MODEL
class Theater
named_scope :with_recent_comments,
:select => "*, recent_comments.created_at AS last_comment_at",
:joins => "INNER JOIN (SELECT * FROM comments WHERE entity_type = 'Theater' ORDER by id DESC LIMIT 100)
AS recent_comments ON recent_comments.entity_id = theaters.id",
:order => "recent_comments.created_at DESC"
end
# CONTROLLER
Theater.with_recent_comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment