Nathan Long
Nathan Long

Reputation: 125912

How can I do a right outer join in a Rails controller?

In a Rails controller, I'm setting a variable like this:

@users = User.find(:all, :joins => family)

This gives me all the users that have families. What I want is exactly the opposite: all the users that don't have families.

I tried adding:

:conditions=> {:family => nil}

... but got an error.

What's the proper way to do a right outer join?

Upvotes: 0

Views: 1517

Answers (3)

KarenG
KarenG

Reputation: 11

If family is a related object, you'd want to check if the family_id is null in the conditions clause.

:conditions => "family_id IS NULL"

Upvotes: 1

Corey
Corey

Reputation: 2243

If you want to do something other than the default inner join you need to pass a string as the :joins parameter with the sql fragment for the join that you want to do.

Upvotes: 0

Dusty
Dusty

Reputation: 2333

Admittedly my Ruby is poor, but I believe when I ran into a similar issue, I was able to filter for NULL using the following

:conditions => "family IS NULL"

Upvotes: 0

Related Questions