Cannot understand why does find_or_create method cannot accept model instances instead of their ids: Friendship.find_or_create_by_user_id_and_friend_id(User.first.id, User.last.id) # works just fine Friendship.find_or_create_by_user_id_and_friend_id(User.first, User.last) # and even Friendship.find_or_create_by_user_id_and_friend_id(User.first, Message.last) rollbacks because of validation (user and friend cannot be the same person): #<Friendship id: nil, user_id: 1, friend_id: 1, state: "pending", created_at: nil, updated_at: nil> So it interprets model instances as true or something like that. I've stopped my search at assign_attributes method of ActiveRecord::Base string send("#{k}=", v) Is this correct behaviour?
Related topics
Topic | Replies | Views | Activity | |
---|---|---|---|---|
find_or_create_by rolls back | 9 | 701 | February 2, 2018 | |
Friendship create action | 0 | 140 | August 19, 2012 | |
Behavior of first_or_create | 4 | 176 | July 25, 2012 | |
_create method doesn't seem to be writing to DB | 2 | 118 | February 15, 2008 | |
has_many & build & first_or_create | 1 | 214 | July 11, 2019 |