views:

77

answers:

1

I have an Author model which habtm :feeds. Using Rails 3 want to setup a scope that finds all authors that have no associated feeds.

class Author < ActiveRecord::Base

    has_and_belongs_to_many :feeds
    scope :without_feed, joins(:feeds).where("authors_feeds.feed_id is null")

end

...doesn't seem to work. It feels like a simple thing. What am I missing here?

A: 

To my knowledge ActiveRecord/Arel do not have a means of defining outer joins. So you'll have to write a bit more SQL than normal. Something like this should do the trick:

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

I am of course guessing at your table names and foreign keys. But that should give you the picture.

bioneuralnet
Yep, that did it. Thank you!
Midwire