# My has\_many has many errors!

**URL:** <https://www.ruby-forum.com/t/my-has-many-has-many-errors/93188>\
**Category:** Rails\
**Created:** [April 28, 2007, 9:26pm UTC](https://www.ruby-forum.com/t/my-has-many-has-many-errors/93188 "2007-04-28T21:26:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [April 28, 2007, 9:26pm UTC](https://www.ruby-forum.com/t/my-has-many-has-many-errors/93188/1 "2007-04-28T21:26:37Z")

</div>

USER  
has\_many categories

CATEGORY  
belongs\_to user  
has\_many notes

NOTES  
belongs\_to category

this is breaking…

@userCategories = Category.find(:all, :conditions =\> “user\_id = 1”,  
:include =\> :notes)

yes, user\_id is a column in my category table…thanks!

---

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [April 28, 2007, 10:09pm UTC](https://www.ruby-forum.com/t/my-has-many-has-many-errors/93188/2 "2007-04-28T22:09:48Z")

</div>

What is the error message given and the SQL generated (You can find  
the SQL from the log)

-Zach

---

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [April 28, 2007, 11:51pm UTC](https://www.ruby-forum.com/t/my-has-many-has-many-errors/93188/3 "2007-04-28T23:51:22Z")

</div>

hi Zach,  
Thanks for the reply…i found my careless error. there ww  
a null value in my db that was causing the error!

---

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [April 29, 2007, 1:16am UTC](https://www.ruby-forum.com/t/my-has-many-has-many-errors/93188/4 "2007-04-29T01:16:59Z")

</div>

Hi there,

I’m actually wondering why you’re finding the categories that belong  
to a given  
user in the manner that you’ve shown us. You’ve gone through all that  
trouble to  
define your user → category relationship why not use it?

user = User.find(1)  
@userCategories = user.categories

I see you’re prefetching notes as well so you can add that to your  
relationship declaration if  
you really need them.

Anyway, not saying your usage isn’t for good reason but given the  
context it sorta jumps out  
at me that you’re not using the association for what’s essentially  
the same exact query as you’re building by hand.

Best regards,

-Michael  
[http://javathehutt.blogspot.com](http://javathehutt.blogspot.com)
