# Newbie question on has\_many

**URL:** <https://www.ruby-forum.com/t/newbie-question-on-has-many/140410>\
**Category:** Rails\
**Created:** [June 13, 2008, 5:39pm UTC](https://www.ruby-forum.com/t/newbie-question-on-has-many/140410 "2008-06-13T17:39:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [June 13, 2008, 5:39pm UTC](https://www.ruby-forum.com/t/newbie-question-on-has-many/140410/1 "2008-06-13T17:39:15Z")

</div>

I have two classes: a Widget and a User. The User has an id that is  
referenced in two places on the Widget: owner\_id and operator\_id.

How should I structure that reference for has\_many?

Thanks folks - I appreciate any help on this.

–David

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [June 13, 2008, 5:42pm UTC](https://www.ruby-forum.com/t/newbie-question-on-has-many/140410/2 "2008-06-13T17:42:11Z")

</div>

widget.rb

has\_many :owners, :class\_name =\> “user”, :foreign\_key =\> “owner\_id”  
has\_many :operators, :class\_name =\> “user”, :foreign\_key =\>  
“operator\_id”

then in the widget database you have those two fields 'owner\_id" and  
“operator\_id”

🙂

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [June 13, 2008, 5:46pm UTC](https://www.ruby-forum.com/t/newbie-question-on-has-many/140410/3 "2008-06-13T17:46:53Z")

</div>

Just what I needed - thanks sw0rdfish. The books I have were not real  
clear on this.

–David

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [June 13, 2008, 6:04pm UTC](https://www.ruby-forum.com/t/newbie-question-on-has-many/140410/4 "2008-06-13T18:04:30Z")

</div>

On 13 Jun 2008, at 16:50, David wrote:

> Hmmm - now that I look at it, should it be belongs to?
> 
> widget.rb
> 
> belongs\_to: owners, :class\_name =\> “user”, :foreign\_key =\> “owner\_id”  
> belongs\_to: operators, :class\_name =\> “user”, :foreign\_key =\>  
> “operator\_id”

it’s belongs\_to (and then the corresponding has\_many on user). You  
need to use singulars though and the class name should be the actual  
class name, ie  
belongs\_to :owner, :class\_name =\> “User” (the foreign key is inferred  
from the association name)

I wrote this up in detail:

> **[Creating multiple associations with the same table - Space Vatican](https://www.spacevatican.org/2008/5/6/creating-multiple-associations-with-the-same-table/)**
>
> Rails makes setting up your associations dead easy and very concise. There’s a whole bunch of options that can be set however as long as you …

Fred

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [June 13, 2008, 9:48pm UTC](https://www.ruby-forum.com/t/newbie-question-on-has-many/140410/5 "2008-06-13T21:48:39Z")

</div>

Yeah Fred got ya… I just based it on your title, and assumed ( shame  
on me ☹ )

If a user has\_many widgets then yeah m use belongs\_to and owner, not  
owners.

Writeup looks good too… have a look at it.

On Jun 13, 12:04 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [June 13, 2008, 10:09pm UTC](https://www.ruby-forum.com/t/newbie-question-on-has-many/140410/6 "2008-06-13T22:09:14Z")

</div>

Fred, great write up - thanks for taking the time to put that up!

–David

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [June 13, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/newbie-question-on-has-many/140410/7 "2008-06-13T17:51:36Z")

</div>

Hmmm - now that I look at it, should it be belongs to?

widget.rb

belongs\_to: owners, :class\_name =\> “user”, :foreign\_key =\> “owner\_id”  
belongs\_to: operators, :class\_name =\> “user”, :foreign\_key =\>  
“operator\_id”

?

Each widget has a single owner and operator. A user can be the owner  
and/or operator of many widgets.

–David
