Hi There, Im trying to get my head around a model association issue that I''ve been struggling with despite the fact that I know I''ve done the exact same thing previously. I have the following models: class User < ActiveRecord::Base has_many :user_mailboxes end class UserMailbox < ActiveRecord::Base belongs_to :user end This all works fine and user maps to usermailbox through user_id. The problem Im having is that the usermailbox table also has a "other_user_id" field which also links back to the users table. I cannot for the life of me figure out how I set this up in the model so that i would be able to call something along the lines of: for item in @user.user_mailboxes item.other_user.first_name end So in essence my issue is that I have two fields in one table pointing back at the same table and need to setup associations. Any help would be greatly appreciated. Cheers, Chris -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
There was recently a discussion of exactly the same scenario here: http://www.ruby-forum.com/topic/969712 That should answer your question. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 1 Feb 2011, at 17:51, Chris Gallagher <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> This all works fine and user maps to usermailbox through user_id. > > The problem Im having is that the usermailbox table also has a > "other_user_id" field which also links back to the users table. I cannot > for the life of me figure out how I set this up in the model so that i > would be able to call something along the lines of: > > for item in @user.user_mailboxes > > item.other_user.first_name > > end > > So in essence my issue is that I have two fields in one table pointing > back at the same table and need to setup associations. >belongs_to :foo, :class_name => ''Bar'' creates an association with Bar usin foo_id as the foreign key Fred> Any help would be greatly appreciated. > > Cheers, > Chris > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Fred, So that means in my user model I can associate by using the following? belongs_to :other_user, :class_name => "UserMailbox" But what about when I want to go back the other way? So I have the mailbox object which has the field "other_user_id" but how do I create the association between this ID and the ID in the users table which is its primary key (id)? Chris -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 1 February 2011 23:01, Chris Gallagher <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi Fred,Please quote the previous message so that the thread is easier to follow.> > So that means in my user model I can associate by using the following? > > belongs_to :other_user, :class_name => "UserMailbox" > > But what about when I want to go back the other way? So I have the > mailbox object which has the field "other_user_id" but how do I create > the association between this ID and the ID in the users table which is > its primary key (id)?Did you read through the thread referenced by Robert? Did you work right through and understand everything there? (Particularly note the use of foreign_key). Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sure did: class User < ActiveRecord::Base has_many :user_mailboxes belongs_to :other_user, :class_name => "UserMailbox" end class UserMailbox < ActiveRecord::Base belongs_to :user belongs_to :other_user, :class_name => "User" end Very helpful guys, Thanks, Chris -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 2 February 2011 22:04, Chris Gallagher <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Sure did:Sure did what? Please quote the previous post so each message makes sense.> > > class User < ActiveRecord::Base > > has_many :user_mailboxes > belongs_to :other_user, :class_name => "UserMailbox"That does not look right.> end > > > class UserMailbox < ActiveRecord::Base > > belongs_to :user > belongs_to :other_user, :class_name => "User"Where is the has_many or has_one at the other end of this? Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.