Reputation: 3019
I have an association with a condition that refers to a different object from the directly loaded object:
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
In my mind, both the association and the directly loaded object should be pointing to the same object. Is this expected behavior?
Here is a gist of the important parts of my model: https://gist.github.com/2635673
Upvotes: 0
Views: 278
Reputation: 10079
This is expected behavior. There is a new feature in rails associations,
:inverse_of
You can set this in the belongs_to and corresponding has_many, then in
user.current_location.users
the occurrence of the user in the current_location.users will be the user object.
But if you get a fresh object from the database, it's a different object.
Upvotes: 1