1

我想为零帖子的博客设置一个 named_scope。

以下不起作用。

class Post < ActiveRecord::Base
  belongs_to :blog
end

class Blog < ActiveRecord::Base
  has_many :posts

  named_scope :has_no_posts, :conditions => "blogs.id NOT IN (SELECT blog_id FROM posts)"
end
4

2 回答 2

0

你确定它不起作用吗?这个对我有用。

于 2010-09-09T13:03:18.987 回答
0

不知道为什么您的代码不起作用。它会出错还是不返回您期望的结果?你可以发布它生成的sql吗?

但是你可以尝试:

named_scope :has_no_posts, :include => [:posts], :conditions => "posts.id IS NULL"
于 2010-09-09T13:11:17.950 回答