so, I''m using following schema: #Users# id nick #Messages# id from_user_id to_user_id subject message #user.rb has_many :messages #message.rb belongs_to :to_user, :class_name => "User" belongs_to :from_user, :class_name => "User" assigning the userids to a message is no problem, but is there a way to collect all messages received by a user? like "user.messages" receives all messages with to_user_id = user.id? -- 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 Jul 7, 2:59 am, Skave Rat <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> so, I''m using following schema: > #user.rb > has_many :messages > > #message.rb > belongs_to :to_user, :class_name => "User" > belongs_to :from_user, :class_name => "User" > > assigning the userids to a message is no problem, but is there a way to > collect all messages received by a user? > like "user.messages" receives all messages with to_user_id = user.id?Your has_many needs to use the :foreign_key option. I put some examples at http://www.spacevatican.org/2008/5/6/creating-multiple-associations-with-the-same-table Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On Jul 7, 2:59�am, Skave Rat <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> like "user.messages" receives all messages with to_user_id = user.id? > Your has_many needs to use the :foreign_key option. I put some > examples at > http://www.spacevatican.org/2008/5/6/creating-multiple-associations-with-the-same-table > > Fredthanks. I forgot to add the associations to user.rb - I just used has_many :messages. It works fine now -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---