Patrick Aljord
2006-Dec-02 18:05 UTC
how to use several foreign key pointing to the same model?
Hey all, I have User has_many posts and Post belongs to a user. I have a Post that can be posted_by a User and deleted_by another User. So I''d like to do something like has_many posters and has_many deleters both pointing to the User models so I can do: @post=Post.find(:first) @deleter=Post.deleter.name pointing to User(@post.deleted_by).name and poster=Post.poster.name pointing to User(@post.posted_by).name thanx in advance 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 -~----------~----~----~----~------~----~------~--~---
Max Muermann
2006-Dec-03 21:34 UTC
Re: how to use several foreign key pointing to the same model?
On 12/3/06, Patrick Aljord <patcito-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hey all, > I have User has_many posts and Post belongs to a user. > I have a Post that can be posted_by a User and deleted_by another User. > So I''d like to do something like has_many posters and has_many > deleters both pointing to the User models so I can do: > @post=Post.find(:first) > @deleter=Post.deleter.name pointing to User(@post.deleted_by).name > and poster=Post.poster.name pointing to User(@post.posted_by).name >You can tell AR which field to use as the foreign key for an association: class Post belongs_to :poster, :class_name=>''User'', :foreign_key=>''poster_id'' belongs_to :deleted_by, :class_name=>''User'', :foreign_key=>''deleted_by_id'' end class User has_many :posts, :foreign_key=>''poster_id'' has_many :deleted_posts, :class_name=>''Post'', :foreign_key=>''deleted_by_id'' end Cheers, Max --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Patrick Aljord
2006-Dec-03 23:48 UTC
Re: how to use several foreign key pointing to the same model?
On 12/3/06, Max Muermann <ruby-DC/T6mWKptNg9hUCZPvPmw@public.gmane.org> wrote:> > On 12/3/06, Patrick Aljord <patcito-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hey all, > > I have User has_many posts and Post belongs to a user. > > I have a Post that can be posted_by a User and deleted_by another User. > > So I''d like to do something like has_many posters and has_many > > deleters both pointing to the User models so I can do: > > @post=Post.find(:first) > > @deleter=Post.deleter.name pointing to User(@post.deleted_by).name > > and poster=Post.poster.name pointing to User(@post.posted_by).name > > > > You can tell AR which field to use as the foreign key for an association: > > class Post > belongs_to :poster, :class_name=>''User'', :foreign_key=>''poster_id'' > belongs_to :deleted_by, :class_name=>''User'', :foreign_key=>''deleted_by_id'' > end > > class User > has_many :posts, :foreign_key=>''poster_id'' > has_many :deleted_posts, :class_name=>''Post'', :foreign_key=>''deleted_by_id'' > end >ok, thanx a lot Max! 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 -~----------~----~----~----~------~----~------~--~---