Hello all (again), Apologies for the incomplete first post. A ''sausage fingers'' error. I''ve (still) got a model (articles, say) that has two fields: created_by, and modified_by. I would like both of these fields to refer to entries in my ''users'' table. I''ve tried the following associations: has_one :user, :foreign_key => "created_by" has_one :user, :foreign_key => "modified_by" but obviously I can''t get at both of them with @article.user. So my question is: is there some way to create these associations? Or should my model be changed? Or will I have to use users.find(@article.created_by) or similar? Any pointers would be welcome. Thanks in advance, F.
Felix, An untested solution in Articles model: has_one :creator, :foreign_key => ''created_by'', :class_name => ''user'' has_one :modifier, :foreign_key => ''modified_by'', :class_name => ''user'' Then from an Article object you could access the user objects by calling Article.creator and Article.modifier. Let me know if it works! -Jeff Felix Ingram wrote:>Hello all (again), > >Apologies for the incomplete first post. A ''sausage fingers'' error. > >I''ve (still) got a model (articles, say) that has two fields: created_by, and >modified_by. I would like both of these fields to refer to entries in >my ''users'' table. > >I''ve tried the following associations: > >has_one :user, :foreign_key => "created_by" >has_one :user, :foreign_key => "modified_by" > >but obviously I can''t get at both of them with @article.user. >So my question is: is there some way to create these associations? Or >should my model be changed? Or will I have to use >users.find(@article.created_by) or similar? > >Any pointers would be welcome. > >Thanks in advance, > >F. >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > >-- Jeff Casimir Developer, CommonText Literacy Project: http://www.commontext.com Teacher, César Chávez PCHS: http://www.cesarchavezhs.org
On 8/6/05, Jeff Casimir <jeff-+RlNNtFrnNmT15sufhRIGw@public.gmane.org> wrote:> Felix, > > An untested solution in Articles model: > > has_one :creator, :foreign_key => ''created_by'', :class_name => ''user'' > has_one :modifier, :foreign_key => ''modified_by'', :class_name => ''user''Would you Adam and Believe it! I works like a charm. I got ''nil object'' errors until I remembered to uppercase the ''u'' in ''user''. Thanks for that. I''ll have to read the api docs more closely the next time: I''d seen this but though it worked the other way round.> Then from an Article object you could access the user objects by calling > Article.creator and Article.modifier. > > Let me know if it works!See above ;-)> -Jeff > > Felix Ingram wrote: > > >Hello all (again), > > > >Apologies for the incomplete first post. A ''sausage fingers'' error. > > > >I''ve (still) got a model (articles, say) that has two fields: created_by, and > >modified_by. I would like both of these fields to refer to entries in > >my ''users'' table. > > > >I''ve tried the following associations: > > > >has_one :user, :foreign_key => "created_by" > >has_one :user, :foreign_key => "modified_by" > > > >but obviously I can''t get at both of them with @article.user. > >So my question is: is there some way to create these associations? Or > >should my model be changed? Or will I have to use > >users.find(@article.created_by) or similar? > > > >Any pointers would be welcome. > > > >Thanks in advance, > > > >F. > >_______________________________________________ > >Rails mailing list > >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > -- > Jeff Casimir > Developer, CommonText Literacy Project: http://www.commontext.com > Teacher, César Chávez PCHS: http://www.cesarchavezhs.org > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >