다음과 같은 연관이 있습니다. 기본적으로 개체 ID가 아니라 사용자 ID를 통해 링크하고 싶습니다.레일스 - 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
은 'personA'입니까? – ErsatzRyan
이렇게 전달됩니다. 그렇습니다 :
–userid를 외래 키로 사용해야하는 이유가 있습니까? 연관된 필드의 기본 키를 사용하지 않는 것이 좋습니다. – jonnii