# Simple Association Issue

**URL:** <https://www.ruby-forum.com/t/simple-association-issue/202566>\
**Category:** Rails\
**Created:** [February 1, 2011, 6:51pm UTC](https://www.ruby-forum.com/t/simple-association-issue/202566 "2011-02-01T18:51:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 1, 2011, 6:51pm UTC](https://www.ruby-forum.com/t/simple-association-issue/202566/1 "2011-02-01T18:51:09Z")

</div>

Hi There,

Im trying to get my head around a model association issue that I’ve been  
struggling with despite the fact that I know I’ve done the exact same  
thing previously.

I have the following models:

class User \< ActiveRecord::Base

has\_many :user\_mailboxes

end

class UserMailbox \< ActiveRecord::Base

belongs\_to :user

end

This all works fine and user maps to usermailbox through user\_id.

The problem Im having is that the usermailbox table also has a  
“other\_user\_id” field which also links back to the users table. I cannot  
for the life of me figure out how I set this up in the model so that i  
would be able to call something along the lines of:

for item in @user.user\_mailboxes

```
item.other_user.first_name

```

end

So in essence my issue is that I have two fields in one table pointing  
back at the same table and need to setup associations.

Any help would be greatly appreciated.

Cheers,  
Chris

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 1, 2011, 7:09pm UTC](https://www.ruby-forum.com/t/simple-association-issue/202566/2 "2011-02-01T19:09:47Z")

</div>

There was recently a discussion of exactly the same scenario here:

[http://www.ruby-forum.com/topic/969712](http://www.ruby-forum.com/topic/969712)

That should answer your question.

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 2, 2011, 12:01am UTC](https://www.ruby-forum.com/t/simple-association-issue/202566/3 "2011-02-02T00:01:25Z")

</div>

Hi Fred,

So that means in my user model I can associate by using the following?

belongs\_to :other\_user, :class\_name =\> “UserMailbox”

But what about when I want to go back the other way? So I have the  
mailbox object which has the field “other\_user\_id” but how do I create  
the association between this ID and the ID in the users table which is  
its primary key (id)?

Chris

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 1, 2011, 9:46pm UTC](https://www.ruby-forum.com/t/simple-association-issue/202566/4 "2011-02-01T21:46:38Z")

</div>

On 1 Feb 2011, at 17:51, Chris G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> So in essence my issue is that I have two fields in one table pointing  
> back at the same table and need to setup associations.

belongs\_to :foo, :class\_name =\> ‘Bar’ creates an association with Bar  
usin foo\_id as the foreign key

Fred

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 2, 2011, 10:26pm UTC](https://www.ruby-forum.com/t/simple-association-issue/202566/5 "2011-02-02T22:26:13Z")

</div>

On 1 February 2011 23:01, Chris G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Fred,

Please quote the previous message so that the thread is easier to  
follow.

> So that means in my user model I can associate by using the following?
> 
> belongs\_to :other\_user, :class\_name =\> “UserMailbox”
> 
> But what about when I want to go back the other way? So I have the  
> mailbox object which has the field “other\_user\_id” but how do I create  
> the association between this ID and the ID in the users table which is  
> its primary key (id)?

Did you read through the thread referenced by Robert? Did you work  
right through and understand everything there? (Particularly note the  
use of foreign\_key).

Colin

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 2, 2011, 11:04pm UTC](https://www.ruby-forum.com/t/simple-association-issue/202566/6 "2011-02-02T23:04:03Z")

</div>

Sure did:

class User \< ActiveRecord::Base

has\_many :user\_mailboxes  
belongs\_to :other\_user, :class\_name =\> “UserMailbox”  
end

class UserMailbox \< ActiveRecord::Base

belongs\_to :user  
belongs\_to :other\_user, :class\_name =\> “User”  
end

Very helpful guys,

Thanks,  
Chris

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 3, 2011, 10:24am UTC](https://www.ruby-forum.com/t/simple-association-issue/202566/7 "2011-02-03T10:24:28Z")

</div>

On 2 February 2011 22:04, Chris G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Sure did:

Sure did what? Please quote the previous post so each message makes  
sense.

> class User \< ActiveRecord::Base
> 
> has\_many :user\_mailboxes  
> belongs\_to :other\_user, :class\_name =\> “UserMailbox”

That does not look right.

> end
> 
> class UserMailbox \< ActiveRecord::Base
> 
> belongs\_to :user  
> belongs\_to :other\_user, :class\_name =\> “User”

Where is the has\_many or has\_one at the other end of this?

Colin
