Specify New Attributes For #find_or_create_by
The ActiveRecord #find_or_create_by
method is a handy way to get an object that represents a record. It will attempt to look up that record, usually based on a unique value or set of values. If it can find one, then that's the record you get. If nothing is found, then it will create a new record.
New records tend to need more data than just the unique lookup attribute. There are a couple ways these other attributes can be specified.
The first is by giving #find_or_create_by
a block.
Another approach is to precede the #find_or_create_by
call with a #create_with
call.
In both cases, the extra attributes will not be applied to the User
record in the case of a find; they are only used in the case of a create.
Last updated