Basically:
Foo.where(attributes).first_or_create
Same as:
Foo.find_or_create_by(attributes)
#first_or_create
sometimes misunderstood because people expect it to be searched for by the given attributes, but this is not so. Aka
Foo.first_or_create(attributes)
Will not look for foo
attributes
. He will accept the first foo
, if any. This is useful if the search terms are a subset of those used to create. Aka
Foo.where(something: value).first_or_create(attributes)
Find the first foo
, where something: value
. If none is present, it will use attributes
to create it.
source share