# Has\_many relationship

**URL:** <https://www.ruby-forum.com/t/has-many-relationship/104196>\
**Category:** Rails\
**Created:** [May 15, 2007, 10:12am UTC](https://www.ruby-forum.com/t/has-many-relationship/104196 "2007-05-15T10:12:51Z")\
**Posts on this page:** 6\
**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:** [May 15, 2007, 10:12am UTC](https://www.ruby-forum.com/t/has-many-relationship/104196/1 "2007-05-15T10:12:51Z")

</div>

I have a basic forum where users can leave messages for other users. I  
did this using a has\_many relationship. I’m able have a user access  
their messages, but I want to be able to display the name of the user  
who sent the message.

CREATE TABLE `users` (  
`id` int(11) NOT NULL auto\_increment primary key,  
`name` varchar default NULL,  
)

CREATE TABLE messages (  
id int NOT NULL auto\_increment primary key,  
sender\_id int default NULL,  
receiver\_id int default NULL,  
message varchar default NULL  
)

class User \< ActiveRecord::Base  
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 \< ActiveRecord::Base  
belongs\_to :sender, :class\_name =\> ‘User’  
belongs\_to :receiver, :class\_name =\> ‘User’  
end

Here I’m able to access all messages with:

\<% @user.received\_messages.each |rec\_message| %\>  
**Message:** \<%= rec\_message.message %\>  
  
  
\<% end %\>

However, I’d also like display the name of the sender. I’d like  
something like:  
**Message:** \<%= rec\_message.message.sender\_id.name %\>  
or  
**Message:** \<%= rec\_message.message.name %\>

Any suggestions? I was thinking of creating a join table with a mysql  
command of:  
select messages.\*, users.name from messages, users where  
messages.sender\_id = users.id;

-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:** [May 15, 2007, 10:44am UTC](https://www.ruby-forum.com/t/has-many-relationship/104196/2 "2007-05-15T10:44:31Z")

</div>

not much big of a deal, you should just re-do your code to use eager  
loading to limit the reqests to the database.

#controller  
@rec\_messages = @user.received\_messages.find :all, :include =\> :sender  
…

#view  
\<% @rec\_messages.each |rec\_message| %\>  
**Message:** \<%= rec\_message.message %\>  
**Sender:** \<%= rec.message.sender.name %\>  
  
  
\<% end %\>  
Message belongs\_to :sender so you can just access it like above…  
the view code i suggest would work without eager loading too, it just  
would hit the database again and again to get the sender info

On 15 Mai, 10:12, Anthony W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<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 19, 2007, 12:51pm UTC](https://www.ruby-forum.com/t/has-many-relationship/104196/3 "2007-05-19T12:51:20Z")

</div>

Thorsten wrote:

> not much big of a deal, you should just re-do your code to use eager  
> loading to limit the reqests to the database.
> 
> #controller  
> @rec\_messages = @user.received\_messages.find :all, :include =\> :sender  
> …
> 
> #view  
> \<% @rec\_messages.each |rec\_message| %\>  
> **Message:** \<%= rec\_message.message %\>  
> **Sender:** \<%= rec\_message.sender.name %\>  
>   
>   
> \<% end %\>  
> Message belongs\_to :sender so you can just access it like above…  
> the view code i suggest would work without eager loading too, it just  
> would hit the database again and again to get the sender info
> 
> On 15 Mai, 10:12, Anthony W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Thanks for the tip Thorsten, but I rails isn’t recognizing sender. You  
mentioned since Message belongs\_to :sender that @rec\_message.sender.name  
should work in accessing the sender’s name. However, when I do a

p @rec\_message.sender

in the controller I get nil in the server console. It seems that rails  
has made the connection for User to Message but not Message to User. Am  
I missing something in my setup?

-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:** [May 21, 2007, 10:34am UTC](https://www.ruby-forum.com/t/has-many-relationship/104196/4 "2007-05-21T10:34:31Z")

</div>

Now that i look at it again, you might want to specify the foreign key  
on the belongs\_to side too…

class Message \< ActiveRecord::Base  
belongs\_to :sender, :class\_name =\> ‘User’, :foreign\_key =\>  
“sender\_id”  
belongs\_to :receiver, :class\_name =\> ‘User’ :foreign\_key =\>  
“receiver\_id”  
end

That should do the trick. At least i hope so 😉

On 19 Mai, 12:51, Anthony W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<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 15, 2007, 10:45am UTC](https://www.ruby-forum.com/t/has-many-relationship/104196/5 "2007-05-15T10:45:31Z")

</div>

dear sender,  
i´m out of the office until may 29th.  
your email will not be forwarded.  
for urgent stuff please contact removed\_email\_address@domain.invalid  
kind regards,  
alexander

---

<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 21, 2007, 4:03pm UTC](https://www.ruby-forum.com/t/has-many-relationship/104196/6 "2007-05-21T16:03:43Z")

</div>

Thorsten wrote:

> Now that i look at it again, you might want to specify the foreign key  
> on the belongs\_to side too…
> 
> class Message \< ActiveRecord::Base  
> belongs\_to :sender, :class\_name =\> ‘User’, :foreign\_key =\>  
> “sender\_id”  
> belongs\_to :receiver, :class\_name =\> ‘User’ :foreign\_key =\>  
> “receiver\_id”  
> end
> 
> That should do the trick. At least i hope so 😉
> 
> On 19 Mai, 12:51, Anthony W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

That did the trick!! Many thanks to you.

-Anthony
