Hi, In my project I have a table with orders (something like that, anyway). I need two references to the users table: one for the creator, and one for the assignee. How do I tell ActiveRecord this, and how am I supposed to store this in my database? Hoping for a solution or tip, and thanks in advance, Jaap
You can do the following: 1. add to your table orders, two columns ''creator_id'' and ''assignee_id'' 2. modify your orders model (under app\models\) has_one :creator, :class_name => ''User'' has_one :assignee :class_name => ''User'' -- Jean-Etienne Mail: etienne dot durand at mail dot com
I did it very similar, but used belongs_to (since in my case, orders has the creator_id as a foreign key) orders model belongs_to :creator, :class_name => ''User'', :foreign_key =>"creator_id" belongs_to :assignee :class_name => ''User'', :foreign_key =>"assignee_id" Then I was able to do orders.creator.username and orders.assignee.username , etc. -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Jean-Etienne Durand Sent: Wednesday, October 19, 2005 9:19 AM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] Multiple use of dbase.user You can do the following: 1. add to your table orders, two columns ''creator_id'' and ''assignee_id'' 2. modify your orders model (under app\models\) has_one :creator, :class_name => ''User'' has_one :assignee :class_name => ''User'' -- Jean-Etienne Mail: etienne dot durand at mail dot com _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Thanx a lot, it did the trick :-) . Regards, Jaap On 10/19/05, Hogan, Brian P. <HOGANBP-VnAisaAFmHY@public.gmane.org> wrote:> I did it very similar, but used belongs_to (since in my case, orders has > the creator_id as a foreign key) > > orders model > belongs_to :creator, :class_name => ''User'', > :foreign_key =>"creator_id" > belongs_to :assignee :class_name => ''User'', > :foreign_key =>"assignee_id" > > Then I was able to do orders.creator.username and > orders.assignee.username , etc. > > > > -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Jean-Etienne > Durand > Sent: Wednesday, October 19, 2005 9:19 AM > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: Re: [Rails] Multiple use of dbase.user > > > You can do the following: > > 1. add to your table orders, two columns ''creator_id'' and ''assignee_id'' > 2. modify your orders model (under app\models\) > has_one :creator, :class_name => ''User'' > has_one :assignee :class_name => ''User'' > > -- > Jean-Etienne > Mail: etienne dot durand at mail dot com > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >