# How do I list items not 'joined' to the current record?

**URL:** <https://www.ruby-forum.com/t/how-do-i-list-items-not-joined-to-the-current-record/74391>\
**Category:** Rails\
**Created:** [November 6, 2006, 9:59am UTC](https://www.ruby-forum.com/t/how-do-i-list-items-not-joined-to-the-current-record/74391 "2006-11-06T09:59:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [November 6, 2006, 9:59am UTC](https://www.ruby-forum.com/t/how-do-i-list-items-not-joined-to-the-current-record/74391/1 "2006-11-06T09:59:58Z")

</div>

My HABTM joins seem to be working for the most part. Yes! Now I’m trying  
to create two lists on my project page of joined and unjoined contacts.

To list the contacts that are joined to my project I use:  
\<% mycontacts = @project.contacts %\>  
then use a for loop to display a checkbox next to each record so I can  
select it to un-join them.

I’m stumped on how to list contacts that are not joined though. I’d like  
to have checkboxes next to them so they can be added to the joined list.  
Eventually I also hoping to have multiple lists of un-joined contacts  
for contractors, developers, designers, and such which can be added to  
the project (filtering the not joined contacts by role).

This starts to work when first loaded but then after addding contacts it  
no longer discriminates and trying to unjoin previous selections stops  
working. Hard coding the project\_id won’t work in the final also:  
\<% mycontacts = Contact.find(:all, :joins =\> ‘INNER JOIN  
contacts\_projects ON contacts.id != contacts\_projects.contact\_id WHERE  
(contacts\_projects.project\_id = 3 )’ ) %\>

Any help appreaciated. Maybe there are some more in-depth HABTM  
tutorials posted somewhere?

DAN

---

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [November 6, 2006, 3:00pm UTC](https://www.ruby-forum.com/t/how-do-i-list-items-not-joined-to-the-current-record/74391/2 "2006-11-06T15:00:44Z")

</div>

Something along the lines of

Contact.find :all, :joins =\> ‘LEFT OUTER JOIN contacts\_projects on  
contacts.id = contacts\_projects.contact\_id’, :conditions =\>  
‘contacts\_projects.project\_id IS NULL’

should get you all contacts which don’t have any projects assigned to  
them.  
Fred

---

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [November 6, 2006, 8:14pm UTC](https://www.ruby-forum.com/t/how-do-i-list-items-not-joined-to-the-current-record/74391/3 "2006-11-06T20:14:21Z")

</div>

Just grab all contacts, and filter out the ones that are included in the  
current project’s contacts. As in…

```
not_my_contacts = Contact.find(:all).collect {|c| c unless 

```

@project.contacts.include?© }

This could be expanded for your future needs…

```
not_my_developers = Contact.find(:all, :conditions => "role = 

```

‘DEVELOPER’").collect {|c| c unless @project.contacts.include?© }

etc.

c.

---

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [December 31, 2007, 11:54am UTC](https://www.ruby-forum.com/t/how-do-i-list-items-not-joined-to-the-current-record/74391/4 "2007-12-31T11:54:36Z")

</div>

Hi Dan

I’ve seen your old post on the rails:talk. I’ve had a similar problem  
and was able to solve it like this:  
@users\_not\_of\_group = User.find\_by\_sql([“select \* from users where  
id not in (select user\_id from groups\_users where group\_id = ?)”,  
params[:id]])  
–\> this gives me all users that are not assigned to a particular  
group

The other method with the filtering did not work for me, I got an  
error about nil-objects. I don’t exactly understand the syntax though,  
my ruby is not so good. It would be very elegant though, I wish I  
could get it to work.

best regards  
paul

ps I meant to post this on your thread, but somehow I could only  
answer to you personally, (thread too old?)
