# Find by association

**URL:** <https://www.ruby-forum.com/t/find-by-association/126248>\
**Category:** Rails\
**Created:** [January 16, 2008, 6:47pm UTC](https://www.ruby-forum.com/t/find-by-association/126248 "2008-01-16T18:47:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DVG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@DVG](https://www.ruby-forum.com/u/DVG)\
**Post date:** [January 16, 2008, 6:47pm UTC](https://www.ruby-forum.com/t/find-by-association/126248/1 "2008-01-16T18:47:02Z")

</div>

First off, I’m an extreme rails newbie trying to find my programming  
legs again after years of not developing.

So, I have a User model which has\_many characters. And a character  
model that belongs\_to the user.

In my Character\_controller, I’m attempting to do a find by association  
to get a list of all characters assocaited with the user.

@user = session[:uid]  
@characters = @user.characters.find\_all\_by\_user(@user.id) (or  
something similar, I don’t have the code in front of me)

When I execute the action, I get an error message stating that there  
is no user\_id field in the character table, which is accurate, but I  
thought the association took care of that. I think I’m missing a step  
somewhere.

---

<div class="post-metadata">

**Author:** ![DVG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@DVG](https://www.ruby-forum.com/u/DVG)\
**Post date:** [January 16, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/find-by-association/126248/2 "2008-01-16T18:54:41Z")

</div>

> When I execute the action, I get an error message stating that there  
> is no user\_id field in the character table, which is accurate, but I  
> thought the association took care of that. I think I’m missing a step  
> somewhere.

You need to create the foreign key (user\_id, in this case) in your  
migration yourself, the association does not automatically do that for  
you.
