In Rails 4 you could do:
GroupMember.find_or_create_by(member_id: 4, group_id: 7)
And use where
is different:
GroupMember.where(member_id: 4, group_id: 7).first_or_create
This will call create
on GroupMember.where(member_id: 4, group_id: 7)
:
GroupMember.where(member_id: 4, group_id: 7).create
On the contrary, the find_or_create_by(member_id: 4, group_id: 7)
will call create
on GroupMember
:
GroupMember.create(member_id: 4, group_id: 7)
Please see this relevant commit on rails/rails.