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| %> <b>Message:</b> <%= rec_message.message %> <br> <% end %> However, I''d also like display the name of the sender. I''d like something like: <b>Message:</b> <%= rec_message.message.sender_id.name %> or <b>Message:</b> <%= 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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| %> <b>Message:</b> <%= rec_message.message %> <b>Sender:</b><%= rec.message.sender.name %> <br> <% 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 Walsh <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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| %> > <b>Message:</b> <%= rec_message.message %> > <br> > <% end %> > > However, I''d also like display the name of the sender. I''d like > something like: > <b>Message:</b> <%= rec_message.message.sender_id.name %> > or > <b>Message:</b> <%= 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 > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dear sender, i´m out of the office until may 29th. your email will not be forwarded. for urgent stuff please contact joern-0M91wEDH++c@public.gmane.org kind regards, alexander --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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| %> > <b>Message:</b> <%= rec_message.message %> > <b>Sender:</b><%= rec_message.sender.name %> > <br> > <% 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 Walsh <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Walsh <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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| %> > > <b>Message:</b> <%= rec_message.message %> > > <b>Sender:</b><%= rec_message.sender.name %> > > <br> > > <% 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 Walsh <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > 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 > > -- > Posted viahttp://www.ruby-forum.com/.- Zitierten Text ausblenden - > > - Zitierten Text anzeigen ---~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Walsh <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>That did the trick!! Many thanks to you. -Anthony -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---