# Help with associations

**URL:** <https://www.ruby-forum.com/t/help-with-associations/120475>\
**Category:** Rails\
**Created:** [November 17, 2007, 10:19am UTC](https://www.ruby-forum.com/t/help-with-associations/120475 "2007-11-17T10:19:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hoolio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f6c823/32.png) [@Hoolio](https://www.ruby-forum.com/u/Hoolio)\
**Post date:** [November 17, 2007, 10:19am UTC](https://www.ruby-forum.com/t/help-with-associations/120475/1 "2007-11-17T10:19:59Z")

</div>

I’m new to RoR and databases in general. Here is my problem.

I have “Lists” and “Items”. Instead of using a join table and a habtm  
relationship, I created a third model so I can hold additional data in  
it. It’s called “Listitems”. I want to be able to display only the  
“Items” that have not yet been added to the certain “List”.

I have:

class List \< ActiveRecord::Base  
has\_many :listitems, :dependent =\> true  
has\_many :items, :through =\> :listitems  
end

class Item \< ActiveRecord::Base  
has\_many :listitems, :dependent =\> true  
has\_many :lists, :through =\> :listitems  
end

class Listitem \< ActiveRecord::Base  
belongs\_to :list  
belongs\_to :item  
end

What kind of query should I be writing in my controller to get the  
results I want.

---

<div class="post-metadata">

**Author:** ![Hoolio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f6c823/32.png) [@Hoolio](https://www.ruby-forum.com/u/Hoolio)\
**Post date:** [November 17, 2007, 12:31pm UTC](https://www.ruby-forum.com/t/help-with-associations/120475/2 "2007-11-17T12:31:46Z")

</div>

Untested, but I think that should do it:

class List  
OTHER\_ITEMS\_SQL = \<\<-EOF  
select \* from items  
where not exists  
(select \* from list\_items.item\_id = items.id and  
list\_items.list\_id = ?)  
EOF  
# Get all items that do NOT belong to this list.  
def other\_items  
Item.find\_by\_sql [OTHER\_ITEMS\_SQL, self.id]  
end  
end

---

<div class="post-metadata">

**Author:** ![Hoolio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f6c823/32.png) [@Hoolio](https://www.ruby-forum.com/u/Hoolio)\
**Post date:** [November 17, 2007, 4:51pm UTC](https://www.ruby-forum.com/t/help-with-associations/120475/3 "2007-11-17T16:51:55Z")

</div>

Hoolio wrote:

> I’m new to RoR and databases in general. Here is my problem.
> 
> I have “Lists” and “Items”. Instead of using a join table and a habtm  
> relationship, I created a third model so I can hold additional data in  
> it. It’s called “Listitems”. I want to be able to display only the  
> “Items” that have not yet been added to the certain “List”.
> 
> I have:
> 
> class List \< ActiveRecord::Base  
> has\_many :listitems, :dependent =\> true  
> has\_many :items, :through =\> :listitems  
> end
> 
> class Item \< ActiveRecord::Base  
> has\_many :listitems, :dependent =\> true  
> has\_many :lists, :through =\> :listitems  
> end
> 
> class Listitem \< ActiveRecord::Base  
> belongs\_to :list  
> belongs\_to :item  
> end
> 
> What kind of query should I be writing in my controller to get the  
> results I want.

Let me encourage you to go “fat model, skinny controller” and write that  
query in the model’s class instead. Just call that method from the  
controller. You’ll thank yourself later. Also, if I were doing it, I’d  
name your join model “Listing”, which falls more trippingly from the  
tongue than “ListItem”.

As for the query, I show how to do that sort of thing here:  
[http://blog.hasmanythrough.com/2006/9/9/finding-unassociated-objects](http://blog.hasmanythrough.com/2006/9/9/finding-unassociated-objects)

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![Hoolio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f6c823/32.png) [@Hoolio](https://www.ruby-forum.com/u/Hoolio)\
**Post date:** [November 17, 2007, 7:47pm UTC](https://www.ruby-forum.com/t/help-with-associations/120475/4 "2007-11-17T19:47:33Z")

</div>

I think I’m still misunderstanding. I read the blogpost and tried  
doing:

class List \< ActiveRecord::Base  
has\_many :items, :through =\> :listitems do  
def not  
find(:conditions =\> [“items.id != ?”, proxy\_owner.id])  
end  
end  
end

I then try looping through

\<% for item in @list.items.not %\>  
\<% item.name %\>  
\<% end%\>

I get the error “Couldn’t find Item without an ID”

On Nov 17, 10:51 am, Josh S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Hoolio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f6c823/32.png) [@Hoolio](https://www.ruby-forum.com/u/Hoolio)\
**Post date:** [November 17, 2007, 8:13pm UTC](https://www.ruby-forum.com/t/help-with-associations/120475/5 "2007-11-17T20:13:25Z")

</div>

I ended up using in my view:

\<% for item in @items %\>  
\<% unless @list.items.include?(item) %\>  
[CODE] etc etc etc

It’s messy, but it seems to work
