Ich habe die folgenden Assoziationen, grundsätzlich möchte ich über Benutzer-ID verknüpfen und nicht die ID des Objekts.Rails - Override Primärschlüssel auf 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
geht 'personA' in Ihre db? – ErsatzRyan
Das geht, also ja ist es:
–Gibt es einen Grund, warum Sie Benutzer-ID als Fremdschlüssel verwenden müssen? Warum nicht einfach den Primärschlüssel des zugehörigen Feldes verwenden? – jonnii