Nat*_*han 3 activerecord model ruby-on-rails
我有一个评论表和一个帖子表,其中一个帖子有很多评论,评论属于一个帖子(即它在表格中有一个post_id).
如何有效地选择没有评论的最后十个帖子.如果没有先选择所有帖子并检查每个帖子的0评论数,我似乎无法做到这一点.
在此先感谢您的帮助.
-Nathan
你可以做
Post.find(:all, :include => :comments, :conditions => "comments.id is null", :limit => 10, :order => "created_at desc")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2216 次 |
| 最近记录: |