raw1z / amistad

Adds friendships management into a rails application
http://github.com/raw1z/amistad
MIT License
185 stars 71 forks source link

Weird issue with friendship table and class/instance equality #24

Open elan opened 12 years ago

elan commented 12 years ago

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.

killthekitten commented 12 years ago

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

killthekitten commented 12 years ago

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.