I''m not getting the result I expect. A question has multiple choices and one answer. I''ve stored the answer id (the id of one of the choices) in the questions table. Here''s my model: class Question < ActiveRecord::Base has_many :choices, :dependent => true # Will be destroyed when this is has_one :answer, :class_name => "Choice" end When in my page I print @question.answer.text, I always see the text of the first choice, not the text of the answer. Indeed, @question.answer_id is always the id of the first choice instead of the value stored in the database field answer_id. What am I doing wrong? Thanks for any help, including pointers to documentation. Jim -- Jim Menard, jimm-Xhj3G7Rj6JI@public.gmane.org, http://www.io.com/~jimm
On 17.2.2005, at 21:48, Jim Menard wrote:> I''m not getting the result I expect. A question has multiple choices > and one answer. I''ve stored the answer id (the id of one of the > choices) in the questions table. Here''s my model: > > class Question < ActiveRecord::Base > has_many :choices, :dependent => true # Will be destroyed when this > is > has_one :answer, :class_name => "Choice" > end > > When in my page I print @question.answer.text, I always see the text > of the first choice, not the text of the answer. Indeed, > @question.answer_id is always the id of the first choice instead of > the value stored in the database field answer_id.This might be totally false, but it might be so that in your second association rails takes the default foreign key field from the :class_name, not from the association name. Thus it would think that your answer is determined by choice_id and somehow get mixed up with the has_many association. Try to add :foreign_key => "answer_id" and see if that helps. //jarkko> > What am I doing wrong? Thanks for any help, including pointers to > documentation. > > Jim > -- > Jim Menard, jimm-Xhj3G7Rj6JI@public.gmane.org, http://www.io.com/~jimm > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 23:28 Thu 17 Feb , Jarkko Laine wrote:> > On 17.2.2005, at 21:48, Jim Menard wrote: > > >I''m not getting the result I expect. A question has multiple choices > >and one answer. I''ve stored the answer id (the id of one of the > >choices) in the questions table. Here''s my model: > > > >class Question < ActiveRecord::Base > > has_many :choices, :dependent => true # Will be destroyed when this > >is > > has_one :answer, :class_name => "Choice" > >end > > > >When in my page I print @question.answer.text, I always see the text > >of the first choice, not the text of the answer. Indeed, > >@question.answer_id is always the id of the first choice instead of > >the value stored in the database field answer_id. > > This might be totally false, but it might be so that in your second > association rails takes the default foreign key field from the > :class_name, not from the association name. Thus it would think that > your answer is determined by choice_id and somehow get mixed up with > the has_many association. > > Try to add :foreign_key => "answer_id" and see if that helps. > > //jarkkoCorrect me if I''m wrong, but I think what you want is "belongs_to", not "has_one". With "has_one", doesn''t the foreign key exist in the _other_ table? Thus, class Question < ActiveRecord::Base ... belongs_to :answer, :class_name => "Choice" ... end should do the trick...shouldn''t it? http://wiki.rubyonrails.com/rails/show/ActiveRecordAssociations I must admit, the usage of belongs_to and has_* can be confusing at times. But perhaps that''s just me. - Jamis -- Jamis Buck jamis_buck-8Bzd4dk9+oo@public.gmane.org http://jamis.jamisbuck.org ------------------------------ "I am Victor of Borge. You will be assimil-nine-ed."
Quoting Jamis Buck <jamis_buck-8Bzd4dk9+oo@public.gmane.org>:> On 23:28 Thu 17 Feb , Jarkko Laine wrote: > > > > On 17.2.2005, at 21:48, Jim Menard wrote: > > > > >class Question < ActiveRecord::Base > > > has_many :choices, :dependent => true # Will be destroyed when this > > >is > > > has_one :answer, :class_name => "Choice" > > >end > > > > This might be totally false, but it might be so that in your second > > association rails takes the default foreign key field from the > > :class_name, not from the association name. Thus it would think that > > your answer is determined by choice_id and somehow get mixed up with > > the has_many association. > > > > Try to add :foreign_key => "answer_id" and see if that helps.You''re correct, the default foreign key name comes from the name of the associated class, and not the name of the association. This seems wrong to me, if I''ve got multiple associations to the same class under different names I have to specify the foreign key for each and every one of them, which is always just the association name with "_id" appended. I''ve been caught by this a couple of times.> I must admit, the usage of belongs_to and has_* can be confusing at > times. But perhaps that''s just me.belongs_to is the _only_ association which has the foreign key in the table belonging to the current class. All of the has_* have the foreign key in some other table. The names are different, ergo the behaviour is different. This is the most sensible arrangement and shouldn''t be that hard to remember once you see the connection. Tim. -- Tim Bates tim-kZbwfhiKUx26c6uEtOJ/EA@public.gmane.org
On 18.2.2005, at 00:32, Jamis Buck wrote:>> > > Correct me if I''m wrong, but I think what you want is "belongs_to", > not "has_one". With "has_one", doesn''t the foreign key exist in the > _other_ table?Oops, definitely. I didn''t pay attention to that at all. Anyway, I think that my foreign_key alert still applies. //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Jarkko Laine wrote:> > This might be totally false, but it might be so that in your second > association rails takes the default foreign key field from the > :class_name, not from the association name. Thus it would think that > your answer is determined by choice_id and somehow get mixed up with > the has_many association. > > Try to add :foreign_key => "answer_id" and see if that helps.That did it. Thank you. I now have has_one :answer, :foreign_key => :answer_id, :class_name => "Choice" and that does what I expect. Jim -- Jim Menard, jimm-Xhj3G7Rj6JI@public.gmane.org, http://www.io.com/~jimm