-
-
Save rubiety/931889 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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' LIMIT 100) | |
AS recent_comments ON recent_comments.entity_id = theaters.id", | |
:order => "recent_comments.created_at DESC" | |
end | |
# CONTROLLER | |
Theater.with_recent_comments | |
# ------------ OR --------------------- | |
# MODEL | |
class Comment | |
named_scope :for_theaters, :conditions => {:entity_type => "Theater"} | |
named_scope :recent, :limit => 100 | |
end | |
class Theater | |
def self.with_recent_comments | |
recent_comments = Comment.recent.for_theaters.all(:select => "DISTINCT entity_id", :order => "id DESC") | |
find(recent_comments.map(&:entity_id)).sort_by {|t| recent_comments.index(t.id)} | |
end | |
end | |
# CONTROLLER | |
@theaters_with_comments = Theater.with_recent_comments | |
# VIEW | |
%h2 Recent comments | |
%ul | |
- @theaters_with_comments[0..9].each do |theater| | |
%li= link_to theater.name, theater_path(theater) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment