# Associations question

**URL:** <https://www.ruby-forum.com/t/associations-question/138934>\
**Category:** Rails\
**Created:** [May 29, 2008, 4:13am UTC](https://www.ruby-forum.com/t/associations-question/138934 "2008-05-29T04:13:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [May 29, 2008, 4:13am UTC](https://www.ruby-forum.com/t/associations-question/138934/1 "2008-05-29T04:13:16Z")

</div>

Hey everyone,

I have a quick question on a tricky model for the app I’m working on.  
I have a Tasks model, and a Task can be assigned to a User; a Task  
should also know the User who assigned (created) it, and be updatable  
by that user. The idea is so the View can have something like on a  
“My Tasks” page:

Assigned by John S. on June 1st, 2008

Since it has to have two references to the same model (User), one for  
who has been assigned to, and one for who did the assigning, what  
association should I be looking at? The associations in Rails are a  
little confusing beyond the basic belongs\_to/has\_one/has\_many things,  
so I’m not sure what I should use. has\_and\_belongs\_to\_many?  
has\_many :through?

Any help would be appreciated.

- Wayne

---

<div class="post-metadata">

**Author:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [May 29, 2008, 4:16am UTC](https://www.ruby-forum.com/t/associations-question/138934/2 "2008-05-29T04:16:12Z")

</div>

fields in tasks: user\_id and assigning\_user\_id

in the task model:

belongs\_to :user  
belongs\_to :assigning\_user, :class =\> “User”

in the user model:

has\_many :tasks  
has\_many :assigned\_tasks, :as =\> “assigning\_user”

On Thu, May 29, 2008 at 11:42 AM, Wayne M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> >

–  
Appreciated my help?  
Recommend me on Working With Rails  
[http://workingwithrails.com/person/11030-ryan-bigg](http://workingwithrails.com/person/11030-ryan-bigg)

---

<div class="post-metadata">

**Author:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [May 29, 2008, 10:44am UTC](https://www.ruby-forum.com/t/associations-question/138934/3 "2008-05-29T10:44:48Z")

</div>

On 29 May 2008, at 03:15, Ryan B. (Radar) wrote:

> has\_many :assigned\_tasks, :as =\> “assigning\_user”

that should be :foreign\_key =\> “assigning\_user\_id”.  
I wrote up a reasonably detailed walkthrough of this sort of thing at

> **[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:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [May 29, 2008, 1:37pm UTC](https://www.ruby-forum.com/t/associations-question/138934/4 "2008-05-29T13:37:08Z")

</div>

I’ll check that link out - thanks all!

- Wayne
