跨模型范围 - Rails 3
我想为没有评论的所有帖子创建一个范围...我不明白如何在模型中(通过创建范围)检查我的帖子是否附加了任何评论,因为只有评论似乎知道什么它们所属的帖子,而不是知道评论属于它的帖子。
Post
has_many :comments
Comments
belong_to :post
(如果我错了,请阻止我。)
I want to create a scope for all Posts without comments... I do not understand how, in the model (by creating a scope), I can check if my Post has any Comments attached to it as only the Comments seem to know what Post they belong to, as opposed to the Post knowing what Comments belong to it.
Post
has_many :comments
Comments
belong_to :post
(Please stop me if I'm wrong.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用sql
,所以范围是
但最好在这里使用
counter_cache
:http ://railscasts.com/episodes/23-counter-cache-columnwith sql
so the scope is
But better to use
counter_cache
here: http://railscasts.com/episodes/23-counter-cache-column