# Has\_many :through question

**URL:** <https://www.ruby-forum.com/t/has-many-through-question/93228>\
**Category:** Rails\
**Created:** [April 29, 2007, 5:57pm UTC](https://www.ruby-forum.com/t/has-many-through-question/93228 "2007-04-29T17:57:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [April 29, 2007, 5:57pm UTC](https://www.ruby-forum.com/t/has-many-through-question/93228/1 "2007-04-29T17:57:25Z")

</div>

I’m working on a simple messaging service where a user can send a  
message to another user. It’s not email, each user can see their  
messages when they login to the site. Now I’m trying to figure out how  
to set up a has\_many : through with two sets. One set is the sender  
having many messages and the other set is all the receiver’s messages.  
Also with two sets I can’t use the normal attribute names like  
message\_id. I need to distinguish between sending and receiving ids. Any  
suggestions?

-Anthony

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [April 29, 2007, 7:24pm UTC](https://www.ruby-forum.com/t/has-many-through-question/93228/2 "2007-04-29T19:24:18Z")

</div>

On 4/29/07, Anthony W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> -Anthony  
> This isn’t has\_many :through, it’s just a simple has\_many association.

class User  
has\_many :sent\_messages, :class\_name =\> ‘Message’, :foreign\_key =\>  
‘sender\_id’  
has\_many :received\_messages, :class\_name =\> ‘Message’, :foreign\_key  
=\> ‘receiver\_id’  
end

class Message  
belongs\_to :sender, :class\_name =\> ‘User’  
belongs\_to :receiver, :class\_name =\> ‘User’  
end

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [May 6, 2007, 1:33pm UTC](https://www.ruby-forum.com/t/has-many-through-question/93228/3 "2007-05-06T13:33:58Z")

</div>

##Controller  
def email  
@user = User.find(params[:id])  
user\_ids = @user.received\_messages.collect {|message|  
message.sender\_id}  
@user\_names = user\_ids.collect {|uzer| User.find(uzer).fname}  
end

##View  
\<% @user.received\_messages.each\_with\_index do |rec\_message, index| %\>  
**From:** \<%= @user\_names[index] %\>  
**Message:** \<%= rec\_message.message %\>  
\<% end %\>

Thanks Rick for the suggestion. With your suggestion I’m able to do a  
basic messaging system. However, I have to look up all the usernames in  
the controller and save them to be displayed in the view to be used the  
‘From’ portions of the message. I’d rather not have to do it this way.  
I’d be better to pull the name when needed. Is there a way so that I  
could do the following.

From: \<%= rec\_message.from.username %\>

-Anthony

Rick O. wrote:

> On 4/29/07, Anthony W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > -Anthony  
> > This isn’t has\_many :through, it’s just a simple has\_many association.
> 
> class User  
> has\_many :sent\_messages, :class\_name =\> ‘Message’, :foreign\_key =\>  
> ‘sender\_id’  
> has\_many :received\_messages, :class\_name =\> ‘Message’, :foreign\_key  
> =\> ‘receiver\_id’  
> end
> 
> class Message  
> belongs\_to :sender, :class\_name =\> ‘User’  
> belongs\_to :receiver, :class\_name =\> ‘User’  
> end
> 
> –  
> Rick O.  
> [http://lighthouseapp.com](http://lighthouseapp.com)  
> [http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
> [http://mephistoblog.com](http://mephistoblog.com)
