Tôi có các liên kết sau, về cơ bản tôi muốn liên kết qua userid chứ không phải id của đối tượng.Đường ray - Ghi đè khóa chính trên has_one
class Tweet < ActiveRecord::Base
has_one :user_profile, :primary_key => 'userid', :foreign_key => 'twitter_userid'
class UserProfile < ActiveRecord::Base
belongs_to :tweet, :foreign_key => 'userid'
However the following spec fails as twitter_userid is reset to the id of the object
it "should have the user's twitter id set on their user profile" do
t = Tweet.new(:twitter_id => 1,
:status => 'Tester',
:userid => 'personA',
:user_profile => UserProfile.new(:twitter_userid => 'personA', :avatar => 'abc'))
t.save!
t.user_profile.twitter_userid.should == 'personA'
end
should have the user's twitter id set on their user profile
expected: "personA",
got: 216 (using ==)
However, the following does pass:
it "should return the correct avatar after being saved" do t = Tweet.new(:twitter_id => 1, :status => 'Tester', :userid => 'personA', :user_profile => UserProfile.new(:twitter_userid => 'personA', :avatar => 'abc')) t.save! t.user_profile.avatar.should == 'abc' end
How can I force it to use userid and not id?
Thanks
Ben
là 'personA' sẽ chuyển vào db của bạn? – ErsatzRyan
Điều này xảy ra, do đó, có nó là:
–Có lý do nào khiến bạn cần sử dụng userid làm khóa ngoại không? Tại sao không chỉ sử dụng khóa chính của trường được liên kết. – jonnii