# Difficult model.find

**URL:** <https://www.ruby-forum.com/t/difficult-model-find/110901>\
**Category:** Rails\
**Created:** [July 31, 2007, 10:33pm UTC](https://www.ruby-forum.com/t/difficult-model-find/110901 "2007-07-31T22:33:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [July 31, 2007, 10:33pm UTC](https://www.ruby-forum.com/t/difficult-model-find/110901/1 "2007-07-31T22:33:27Z")

</div>

Hi all!

I have this declaration model: [Parked at Loopia](http://pastie.caboo.se/83840)

What I want to do:  
get all users that not belong to the declaration.

I could do a loop. First get all the users. I know who belong to the  
declaration, so I also know who does not.  
But this seems like a lot of looping.

Is there an easy way I can get all those users that NOT belong to the  
declaration? I looked into using joins in my find, but I don’t know how  
to use that in this context.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [August 1, 2007, 4:32pm UTC](https://www.ruby-forum.com/t/difficult-model-find/110901/2 "2007-08-01T16:32:36Z")

</div>

I can only think of doing it in Ruby:

def other\_users  
User.find(:all) - self.users  
end

there must be a more efficient way to do it via ActiveRecord  
(therefore, only one query), something like

def other\_users  
User.find :all, :conditions =\> [“declaration\_id \<\> ?”, self.id]  
end

(or something like that, you get the idea, it’s awfully not  
abstracted).

Bye,

nachokb

On Jul 31, 5:33 pm, Leon B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [August 2, 2007, 3:00am UTC](https://www.ruby-forum.com/t/difficult-model-find/110901/3 "2007-08-02T03:00:44Z")

</div>

Leon, I think a left outer join of Users on Declarations would do  
it.

in SQL that would be:  
SELECT \* from Users left outer join Declarations ON Users.user\_id =  
Declarations.user\_id WHERE Declarations.user\_id IS NULL

in ActiveRecord, an :includes does a left outer join so (I may not get  
this 100% right)  
User.find(:all, :conditions =\> “declarations.user\_id is  
NULL”, :include =\> “declarations”)

Cheers, --Kip

On Aug 1, 4:33 am, Leon B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
