Try this:
class Post < ActiveRecord::Base
has_many :comments, :as => :commentable
named_scope :with_comments, :joins => :comments,
:order => "comments.created_at DESC"
end
class Video < ActiveRecord::Base
has_many :comments, :as => :commentable
named_scope :with_comments, :joins => :comments,
:order => "comments.created_at DESC"
end
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
Now you can run these commands:
Post.with_comments
Video.with_comments
Edit
It looks like you want a single list with Videos and Posts. This is quite tricky, but doable. For every page you will have to execute 3 queries.
def commented_videos_posts(page = 1, page_size = 30)
# query 1: get the lastest comments for posts and videos
comments = Comment.find_all_by_commentable_type(["Post", "Video"],
:select => "commentable_type, commentable_id,
MAX(created_at) AS created_at",
:group => "commentable_type, commentable_id"
:order => "created_at DESC",
:limit => page_size, :offset => (page-1)*page_size)
# get the video and post ids
post_ids, video_ids = [], []
comments.each do |c|
post_ids << c.commentable_id if c.commentable_type == "Post"
video_ids << c.commentable_id if c.commentable_type == "Video"
end
posts, videos = {}, {}
# query 2: get posts
Post.all(post_ids).each{|p| posts[p.id] = p }
# query 3: get videos
Video.all(video_ids).each{|v| videos[v.id] = v }
# form one list of videos and posts
comments.collect do |c|
c.commentable_type == "Post" ? posts[c.commentable_id] :
videos[c.commentable_id]
end
end