# Table associated problem using belongs\_to

**URL:** <https://www.ruby-forum.com/t/table-associated-problem-using-belongs-to/156854>\
**Category:** Rails\
**Created:** [January 4, 2009, 9:20am UTC](https://www.ruby-forum.com/t/table-associated-problem-using-belongs-to/156854 "2009-01-04T09:20:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rays](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7ab992/32.png) [@rays](https://www.ruby-forum.com/u/rays)\
**Post date:** [January 4, 2009, 9:20am UTC](https://www.ruby-forum.com/t/table-associated-problem-using-belongs-to/156854/1 "2009-01-04T09:20:15Z")

</div>

## I have two tables: Table [packages] id name user\_name

## 1 PK1 Ray

## Table [users] id name full\_name

## 2 Ray Ray Sun

My purpose is to find the full name when I find packages. So I modify  
Model Package to:

class Package \< ActiveRecord::Base  
belongs\_to :user,  
:class\_name =\> “User”,  
:foreign\_key =\> “user\_name”  
end

Then I run Package.find(1, :include =\> :user) in Rails console. From the  
log, I can see the sql is:  
Package Load Including Associations (0.000309) SELECT  
`packages`.`id` AS t0\_r0, `packages`.`name` AS t0\_r1,  
`packages`.`user_name` AS t0\_r2, `users`.`id` AS t1\_r0, `users`.`name`  
AS t1\_r1, `users`.`full_name` AS t1\_r2 FROM `packages` LEFT OUTER JOIN  
`users` ON `users`.id = `packages`.user\_name WHERE (`packages`.`id` = 1)

But I cannot find the parameters for belongs\_to to set  
associated\_foreign\_key like has\_and\_belongs\_to\_many. I don’t know how to  
deal with it. Please help. Thanks.

---

<div class="post-metadata">

**Author:** ![rays](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7ab992/32.png) [@rays](https://www.ruby-forum.com/u/rays)\
**Post date:** [January 4, 2009, 2:35pm UTC](https://www.ruby-forum.com/t/table-associated-problem-using-belongs-to/156854/2 "2009-01-04T14:35:58Z")

</div>

On 4 Jan 2009, at 08:20, Ray Sun wrote:

> But I cannot find the parameters for belongs\_to to set  
> associated\_foreign\_key like has\_and\_belongs\_to\_many. I don’t know  
> how to  
> deal with it. Please help. Thanks.

There is no such option for belongs\_to. In the rails world your  
packages table would have a user\_id column.

Fred

---

<div class="post-metadata">

**Author:** ![rays](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7ab992/32.png) [@rays](https://www.ruby-forum.com/u/rays)\
**Post date:** [January 4, 2009, 2:39pm UTC](https://www.ruby-forum.com/t/table-associated-problem-using-belongs-to/156854/3 "2009-01-04T14:39:40Z")

</div>

Frederick C. wrote:

> On 4 Jan 2009, at 08:20, Ray Sun wrote:
> 
> > But I cannot find the parameters for belongs\_to to set  
> > associated\_foreign\_key like has\_and\_belongs\_to\_many. I don’t know  
> > how to  
> > deal with it. Please help. Thanks.
> 
> There is no such option for belongs\_to. In the rails world your  
> packages table would have a user\_id column.
> 
> Fred

Is there any way as workaround to satisfy my requirement? Thanks.

---

<div class="post-metadata">

**Author:** ![rays](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7ab992/32.png) [@rays](https://www.ruby-forum.com/u/rays)\
**Post date:** [January 4, 2009, 5:33pm UTC](https://www.ruby-forum.com/t/table-associated-problem-using-belongs-to/156854/4 "2009-01-04T17:33:35Z")

</div>

On 4 Jan 2009, at 13:39, Ray Sun wrote:

> > packages table would have a user\_id column.
> > 
> > Fred
> 
> Is there any way as workaround to satisfy my requirement? Thanks.

Nothing obvious springs to mind.

Fred
