Hi, I have a model tests_user and score which are associated with each other by has_one and belongs_to association. class TestsUser < ActiveRecord::Base has_one :score, :foreign_key => :reg_no end class TestsUser < ActiveRecord::Base belongs_to :tests_user, :foreign_key => :reg_no end where reg_no is a unique field in both the table! score = Score.find(:first) gives me the first record from score table *Now as i do score.tests_user it gives nill.* and same with if i do test_user = TestsUser.find(:first) * then test_user.score is showing as nil...* * * * * Is this issue because of the foreign_key is not the default id!!!! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/HmuXi4iXzGAJ. 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.
class TestUser has_one :score end class Score belongs_to :test_user end score table will has id,test_user_id,score if you want to get a user score just give test_user.score -- 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 16 September 2011 13:56, mohitnegi <mohitnegi85-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > I have a model tests_user and score which are associated with each other by > has_one and belongs_to association. > class TestsUser < ActiveRecord::Base > has_one :score, :foreign_key => :reg_no > end > class TestsUser < ActiveRecord::BaseThat should be class Score.> belongs_to :tests_user, :foreign_key => :reg_no > end > where reg_no is a unique field in both the table!You only need reg_no in the scores table> score = Score.find(:first) gives me the first record from score table > > Now as i do score.tests_user it gives nill. > and same with if i do > test_user = TestsUser.find(:first) > then test_user.score is showing as nil... > > Is this issue because of the foreign_key is not the default id!!!!There should be no problem with a non-default foreign key. What do you see if you do score = Score.find(:first).reg_no It should be an id of a TestsUser Colin -- gplus.to/clanlaw -- 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.
On Sep 16, 1:56 pm, mohitnegi <mohitneg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I have a model tests_user and score which are associated with each other by > has_one and belongs_to association. > > class TestsUser < ActiveRecord::Base > has_one :score, :foreign_key => :reg_no > end > > class TestsUser < ActiveRecord::Base > belongs_to :tests_user, :foreign_key => :reg_no > end > > where reg_no is a unique field in both the table!In addition to what colin has said, with what you have there rails will assume there is a reg_no column on the tests_user table which references the id column on the other table. If it needs to point at a column other that id then you need to use the primary_key option in addition to the foreign_key option. Fred> > score = Score.find(:first) gives me the first record from score table > > *Now as i do score.tests_user it gives nill.* > > and same with if i do > test_user = TestsUser.find(:first) > * then test_user.score is showing as nil...* > * > * > * > * > Is this issue because of the foreign_key is not the default id!!!!-- 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.
Thanks all, Actually reg_no column is present in both the models. and was not mentioned as primary key in any of the model. Thanks Colin , Fred for the quick response!! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/dkqM24h7qFMJ. 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.