amistad icon indicating copy to clipboard operation
amistad copied to clipboard

Weird issue with friendship table and class/instance equality

Open elan opened this issue 12 years ago • 2 comments

The invited_by? and invited? methods aren't working for me because of this line:

friendship.friendable == user

For some reason when I access a User instance via the association, even though I get something back which appears to be the same class, but isn't, iow (assuming the friendable ID is 1):

User.find(1) == friendship.friendable # false!

This is under Webrick, Rails 3.0.10, Amistad 0.8.

The most bizarre thing is that it works in the Rails console just fine, and it works in Webrick ONCE before failing.

I hesitated to post this because I am clearly insane, but it's also 100% reproducible.

elan avatar Apr 08 '12 08:04 elan

+1 from me, it's weird thing. In console it works great, but not in real application.

killthekitten avatar Jun 15 '12 21:06 killthekitten

I think it's because of identifying objects in ruby\rails. User, which been called as User.find(1) has one object id, and the other user, from friendship.friendable is the other, so comparing them is meaningless.

I forked amistad, and fixed this bug by comparing User.id and friendable.id. But also, I think that using :inverse_of in relations can do the same.

killthekitten avatar Jun 15 '12 21:06 killthekitten