如何使用Rails 3范围过滤关联记录不存在的habtm连接表?

Mid*_*ire 13 activerecord scope join where ruby-on-rails-3

我有一个Authorhabtm模型:feed.使用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
Run Code Online (Sandbox Code Playgroud)

......似乎没有用.感觉就像一件简单的事情.我在这里错过了什么?

bio*_*net 23

据我所知,ActiveRecord/Arel没有定义外连接的方法.因此,您必须编写比正常情况更多的SQL.像这样的东西应该做的伎俩:

    scope :without_feed, joins('left outer join authors_feeds on authors.id=authors_feeds.author_id').where('authors_feeds.feed_id is null')
Run Code Online (Sandbox Code Playgroud)

我当然猜测你的表名和外键.但这应该会给你的照片.