HI everybody, I need some help with model association. Here I have one user table. User table. id name 1 XXX 2 YYY 3 ZZZ 4 XYZ Message Table id sender_id receiver_id message 1 1 2 HI. HOW R U 2 2 1 YEAH. FINE 3 2 4 HI. WR R U NOW? 1. Here I want get the sender_name and receiver name for an message. Like below @msg=Message.find(1) @msg.sender.name @msg.receiver.name 2. Same like I want to get the sent and received messages for a Particular message. @user=User.find(1) @user.sent_msg @user.received_msg Can any one help how I have to build the association between these two models, Here I dont want to create one more table. Regards, T.Veeraa. -- 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 -~----------~----~----~----~------~----~------~--~---
On Dec 12, 2007 8:20 PM, Veera Sundaravel <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > > HI everybody, > > I need some help with model association. > > Here I have one user table. > > User table. > id name > 1 XXX > 2 YYY > 3 ZZZ > 4 XYZ > > Message Table > > id sender_id receiver_id message > 1 1 2 HI. HOW R U > 2 2 1 YEAH. FINE > 3 2 4 HI. WR R U NOW? > > > 1. Here I want get the sender_name and receiver name for an message. > Like below > @msg=Message.find(1) > @msg.sender.name > @msg.receiver.nameclass Message < ActiveRecord::Base belongs_to :sender, :class_name => "User" belongs_to :receiver, :class_name => "User" end> 2. Same like I want to get the sent and received messages for a > Particular message. > @user=User.find(1) > @user.sent_msg > @user.received_msgclass 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 Pat --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Pat Maddox wrote:> On Dec 12, 2007 8:20 PM, Veera Sundaravel > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> id name >> 3 2 4 HI. WR R U NOW? >> >> >> 1. Here I want get the sender_name and receiver name for an message. >> Like below >> @msg=Message.find(1) >> @msg.sender.name >> @msg.receiver.name > > class Message < ActiveRecord::Base > belongs_to :sender, :class_name => "User" > belongs_to :receiver, :class_name => "User" > end > > >> 2. Same like I want to get the sent and received messages for a >> Particular message. >> @user=User.find(1) >> @user.sent_msg >> @user.received_msg > > 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 > > Pat@user=User.find(1) @user.sent_messages working. But @msg = Message.find(1) @msg.sender returns nil -- 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 -~----------~----~----~----~------~----~------~--~---
class Message < ActiveRecord::Base belongs_to :sender, :class_name => "User", :foreign_key=>"id" belongs_to :receiver, :class_name => "User", :foreign_key=>"id" end 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 Working fine. Now I can query from both the ends. Thanks for your help Pat Maddox. Regards, Veeraa.T -- 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 -~----------~----~----~----~------~----~------~--~---
sorry for the mistake 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 Veera Sundaravel wrote:> class Message < ActiveRecord::Base > belongs_to :sender, :class_name => "User", :foreign_key=>"id" > belongs_to :receiver, :class_name => "User", :foreign_key=>"id" > end > > > 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 > > > Working fine. > > Now I can query from both the ends. > > Thanks for your help Pat Maddox. > > Regards, > Veeraa.T-- 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 -~----------~----~----~----~------~----~------~--~---