-
Notifications
You must be signed in to change notification settings - Fork 30
Open
Description
Description
Let's introduce a new #update_or_create method to query sets.
Rationale
Let's make it easy to either update an existing record or create a new one with a set of specified attributes through the use of a new #update_or_create query set method. A simple use of this method would look something like this:
Person.all.update_or_create(updates: { first_name: "Bob" }, first_name: "John", last_name: "Doe")By default, the set of attributes specified in the updates argument should be used to update the existing record or initialize the newly created record.
Alternatively, it should be possible to specify an alternative defaults argument that should be used instead of the updates one for initializing newly created records (this is important if additional attributes must be set on new records but are not necessary for updated records):
Person.all.update_or_create(
updates: { first_name: "Bob" },
defaults: { first_name: "Bob", active: true },
first_name: "John",
last_name: "Doe"
)