# Multiple foreignkeys for same model with different meaning (User.received\_messages / User.sent\_mess

**URL:** <https://www.ruby-forum.com/t/multiple-foreignkeys-for-same-model-with-different-meaning-user-received-messages-user-sent-mess/161917>\
**Category:** Rails\
**Created:** [March 3, 2009, 8:51pm UTC](https://www.ruby-forum.com/t/multiple-foreignkeys-for-same-model-with-different-meaning-user-received-messages-user-sent-mess/161917 "2009-03-03T20:51:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![LpLpLp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@LpLpLp](https://www.ruby-forum.com/u/LpLpLp)\
**Post date:** [March 3, 2009, 8:51pm UTC](https://www.ruby-forum.com/t/multiple-foreignkeys-for-same-model-with-different-meaning-user-received-messages-user-sent-mess/161917/1 "2009-03-03T20:51:25Z")

</div>

Hello,

I have 2 models: User and Message

Message:  
belongs\_to :sender, :class\_name=\>‘User’, :foreign\_key=\>‘sender\_id’  
belongs\_to :receiver, :class\_name=\>‘User’, :foreign\_key=\>‘receiver\_id’

// MESSAGE-MIGRATION START  
create\_table :messages do |t|  
t.integer :sender\_id  
t.integer :receiver\_id  
t.string :subject  
t.text :content

```
  t.timestamps
end

```

// MIGRATION END

User:  
has\_many :sent\_messages, :class\_name =\> “Message”, :foreign\_key =\>  
“sender\_id”  
has\_many :received\_messages, :class\_name =\> “Message”, :foreign\_key  
=\> “receiver\_id”

I want to access User.messages, because I need it for the destroy-  
action -\> User.messages.find(params[:id])

Can anyone help me?  
Thanks!

---

<div class="post-metadata">

**Author:** ![LpLpLp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@LpLpLp](https://www.ruby-forum.com/u/LpLpLp)\
**Post date:** [March 3, 2009, 8:57pm UTC](https://www.ruby-forum.com/t/multiple-foreignkeys-for-same-model-with-different-meaning-user-received-messages-user-sent-mess/161917/2 "2009-03-03T20:57:55Z")

</div>

Hmmm…

It worked with:

has\_many :friendships, :class\_name =\> “Friendship”, :foreign\_key =\>  
“sender\_id = #{self.id} or friendships.receiver\_id = #{self.id} AND 1”

But thats not very nice…!!! Is there a better solution??

---

<div class="post-metadata">

**Author:** ![LpLpLp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@LpLpLp](https://www.ruby-forum.com/u/LpLpLp)\
**Post date:** [March 3, 2009, 8:59pm UTC](https://www.ruby-forum.com/t/multiple-foreignkeys-for-same-model-with-different-meaning-user-received-messages-user-sent-mess/161917/3 "2009-03-03T20:59:42Z")

</div>

On Mar 3, 7:50 pm, Lp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I want to access User.messages, because I need it for the destroy-  
> action → User.messages.find(params[:id])

You want a single association that covers both sent and received  
messages ? AFAIL there is no way to get an association to do that  
(although you could construct a named scope that did).

Fred

---

<div class="post-metadata">

**Author:** ![LpLpLp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@LpLpLp](https://www.ruby-forum.com/u/LpLpLp)\
**Post date:** [March 3, 2009, 9:03pm UTC](https://www.ruby-forum.com/t/multiple-foreignkeys-for-same-model-with-different-meaning-user-received-messages-user-sent-mess/161917/4 "2009-03-03T21:03:16Z")

</div>

Hm, I tried it with named scopes, but

User.rb:

named\_scope :messages, :join =\> :messages, :conditions = [“sender\_id  
= ? or receiver\_id = ?”, self.id, self.id]

returns the collection of USERS -\> not messages!

Or is there chance to get the messages?

---

<div class="post-metadata">

**Author:** ![LpLpLp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@LpLpLp](https://www.ruby-forum.com/u/LpLpLp)\
**Post date:** [March 3, 2009, 9:16pm UTC](https://www.ruby-forum.com/t/multiple-foreignkeys-for-same-model-with-different-meaning-user-received-messages-user-sent-mess/161917/5 "2009-03-03T21:16:26Z")

</div>

On Mar 3, 8:02 pm, Lp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hm, I tried it with named scopes, but
> 
> User.rb:
> 
> named\_scope :messages, :join =\> :messages, :conditions = [“sender\_id  
> = ? or receiver\_id = ?”, self.id, self.id]
> 
> returns the collection of USERS → not messages!
> 
> Or is there chance to get the messages?

You’d have to put the scope on Message class if you want it to return  
messages ie  
named\_scope :for\_user, lambda {|u| :conditions =\> [“sender\_id = ? or  
received\_id = ?”, u,u]}

Then Message.for\_user(bob) returns a scope for bob’s messages. You  
could define a messages method on user to make this flow slightly  
nicer ie

def messages  
Message.for\_user(self)  
end

Fred

---

<div class="post-metadata">

**Author:** ![LpLpLp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@LpLpLp](https://www.ruby-forum.com/u/LpLpLp)\
**Post date:** [March 3, 2009, 9:22pm UTC](https://www.ruby-forum.com/t/multiple-foreignkeys-for-same-model-with-different-meaning-user-received-messages-user-sent-mess/161917/6 "2009-03-03T21:22:53Z")

</div>

Cool, thx!

Nice idea…
