【发布时间】:2011-04-21 23:49:44
【问题描述】:
我有一个Author 模型,它是 habtm :feeds。使用 Rails 3 想要设置一个范围来查找所有没有关联提要的作者。
class Author < ActiveRecord::Base
has_and_belongs_to_many :feeds
scope :without_feed, joins(:feeds).where("authors_feeds.feed_id is null")
end
...似乎不起作用。感觉是一件很简单的事情。我在这里错过了什么?
【问题讨论】:
-
感觉就像 joins(:feeds) 正在执行内部连接,只有在他们首先有提要时才会选择作者?
-
它确实在做一个内连接:
SELECT authors.* FROM authors INNER JOIN authors_feeds ON authors_feeds.author_id = authors.id INNER JOIN feeds ON feeds.id = authors_feeds.feed_id WHERE (authors_feeds.feed_id is null)而我想要的是一个外连接。有什么帮助吗?
标签: activerecord ruby-on-rails-3 scope join where