Has_many relationship

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

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. [email protected]

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. [email protected]

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

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 :wink:

On 19 Mai, 12:51, Anthony W. [email protected]

dear sender,
i´m out of the office until may 29th.
your email will not be forwarded.
for urgent stuff please contact [email protected]
kind regards,
alexander

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 :wink:

On 19 Mai, 12:51, Anthony W. [email protected]

That did the trick!! Many thanks to you.

-Anthony