0

直接ロードされたオブジェクトとは異なるオブジェクトを参照する条件との関連付けがあります。

it "should point to the same object" do
  user = create(:user)
  user.current_location.should == nil

  user.update_location(latitude: 11, longitude: 22)
  user.current_location.should_not == nil

  location = UserLocation.first

  location.id.should == user.current_location.id
  location.object_id.should == user.current_location.object_id #fails on this line
end

私の考えでは、関連付けと直接ロードされたオブジェクトの両方が同じオブジェクトを指している必要があります。これは予想される動作ですか?

これが私のモデルの重要な部分の要点です: https ://gist.github.com/2635673

4

1 に答える 1

1

これは予想される動作です。Railsアソシエーションには新機能があります。

:inverse_of

これはbelongs_toおよび対応するhas_manyで設定でき、次に

user.current_location.users

current_location.usersでのユーザーの出現は、ユーザーオブジェクトになります。

ただし、データベースから新しいオブジェクトを取得した場合、それは別のオブジェクトです。

于 2012-05-08T22:40:20.783 に答える