2009-08-27 6 views
0

Railsの一般的なユーザー - 投稿モデルでは、すべてのユーザーが投稿を作成してコメントを作成することができ、質問は特定の投稿のすべてのユーザーの最新コメントを取得する方法です。それを行うにはどのようにRailsの最新のコメントのみを取得

Post A have 3 user making comment 
User 1 latest comment that is 6 
User 2 latest comment that is 4 
user 3 latest comment that is 2 

Example: 

Post A have 3 user making comment 
User 1 have comment 1, 2, 3, 4, 5, 6 
User 2 have comment 1, 2, 3, 4 
User 3 have comment 1, 2 

だから私はしたいビューは、すべてのユーザーのためのちょうど最新コメントのですか?

おかげで、このような

+0

そのPostモデルのhas_manyのユーザー、またはそれhas_manyのコメントはそのこれは、ユーザーに帰属しますか? – Matchu

+0

ユーザーには多くの投稿とコメントがあり、投稿は多くのコメントを持ち、ユーザーに属しています。コメントは投稿に属し、ユーザーに属します。 – gkrdvl

答えて

1

何か:

post.comments.for_user(current_user).last 

は、モデルトリックを行う必要があり

class Comment 
    named_scope :for_user, lambda{ |user| {:conditions=>{:user_id => user.id}} 
end 

でnamed_scopeを追加します。私が持っていた

messages_by_users = post.messages.group_by(&:user) 

messages_by_users.each do |key, value| 
    messages_by_users[key] = value.last 
end 
+0

最新のコメントを表示したい場合は、この仕事を完了する1つの方法がありますか?すべての3人のユーザー... – Matchu

+0

Matchu、私は一度だけクエリを読み込むレールソリューションを追加しました。メモリにすべてのコメントを読み込むことで)、SQLを使用した他のより良い方法があるかと思います –

+0

@ez、最初の解決策はcurrent_userですコメント、どのように他のものをつかむために?最初のソリューションのために – gkrdvl

0

あなたはむしろレールでそれを行う場合

は、この種のデータを取得するには、通常、私は2つのクエリを実行してしまいます。私の場合、私はブログとその投稿を持っています.3つの最新のブログ投稿のリストが必要でしたが、ブログがユニークであるという制限がありました。同じブログの2つの投稿は必要ありません。だからあなたの場合には、あなたのようなものかもしれません

q = <<-EOQ 
SELECT id,pub_date FROM 
    (
    SELECT id,blog_id,pub_date 
    FROM posts 
    ORDER BY pub_date DESC 
    LIMIT 40 
) 
t 
GROUP BY blog_id 
ORDER BY pub_date DESC 
LIMIT #{num_posts} 
EOQ 
post_ids = Post.connection.select_values(q) 
Post.find(:all, :include => [:blog], :conditions => ["id IN (?)", post_ids], :order => "posts.pub_date DESC")  

:データベースがコメントにシーケンシャルIDを割り当てると仮定すると

q = <<-EOQ 
SELECT id FROM 
    (
    SELECT id,post_id 
    FROM comments 
    ORDER BY id DESC 
    LIMIT 40 
) 
t 
GROUP BY post_id 
ORDER BY id DESC 
LIMIT 10 
EOQ 
post_ids = Post.connection.select_values(q) 
Post.find(:all, :include => [:blog], :conditions => ["id IN (?)", post_ids], :order => "posts.id DESC") 
0

を、あなたが行うことができます私はこのような何か(MySQLを)やってしまいましたこれは:

class Comment 
    named_scope :most_recent, lambda { 
    lastest_comments = Comment.maximum :id, :group => "user_id, post_id" 
    { :conditions => [ "comment_id in ?", lastest_comments.map(&:last) ] } 
    } 
end 

これは、さまざまな方法で使用できる2つのクエリ方法を提供します。上記のnamed_scopeは、すべての投稿のすべてのユーザーの最新のコメントを取り戻します。これはデータベースが巨大な場合には問題になるかもしれませんが、より具体的にするための条件を追加することは可能です。現状では

は、それはあなたが次の操作を行うことを可能にする柔軟な方法である:ポストAは、3人のユーザーがいるときに、あなたが言っている

Comment.most_recent.find_by_user @user #-> the most recent comments on all posts by a user 
@user.comments.most_recent    #-> same as above 

Comment.most_recent.find_by_post @post #-> the most recent comments on a single post by all users 
@post.comments.most_recent    #-> same as above 

Comment.most_recent.find_by_user_and_post @user, @post #-> the specific most recent comment by a certain user on a certain post 
@post.comments.most_recent.find_by_user @user    #-> you get the idea 
関連する問題