If there is data specified in the argument condition, that data is returned. If not, create a new one.

def find_or_create_by(attributes, &block)
  find_by(attributes) || create(attributes, &block) 


The result is idempotent. Idempotence means that the results obtained are the same no matter how many times you go.

If the specified data already exists, it will not be newly created, so Even if you repeat find_or_create_by, the results you get are equal.

On the other hand, if it was a create method, It can be said that it is not idempotent because new creations are made as much as it is repeated.

Recommended Posts