Skip to content

Instantly share code, notes, and snippets.

@rubiety
Forked from mokolabs/gist:932037
Created April 20, 2011 17:31
Show Gist options
  • Save rubiety/932081 to your computer and use it in GitHub Desktop.
Save rubiety/932081 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 id, entity_id, created_at 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