-
Notifications
You must be signed in to change notification settings - Fork 29
Implement update_or_create method
#337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1577,6 +1577,46 @@ module Marten | |||||
| update(kwargs.to_h) | ||||||
| end | ||||||
|
|
||||||
| # Updates a model record matching the given filters or creates a new one if no one is found. | ||||||
| # | ||||||
| # This method first attempts to retrieve a record that matches the specified filters. If it exists, the record | ||||||
| # is updated using the attributes provided via the required `updates` argument. If no matching record is found, | ||||||
| # a new one is created using the `updates` attributes: | ||||||
| # | ||||||
| # ``` | ||||||
| # person = Person.all.update_or_create(updates: {first_name: "Bob"}, first_name: "John", last_name: "Doe") | ||||||
| # ``` | ||||||
| # | ||||||
| # If additional attributes should only be used when creating new records, a `defaults` argument can be provided: | ||||||
| # | ||||||
| # ``` | ||||||
| # person = Person.all.update_or_create( | ||||||
| # updates: {first_name: "Bob"}, | ||||||
| # defaults: {first_name: "Bob", active: true}, | ||||||
| # first_name: "John", | ||||||
| # last_name: "Doe" | ||||||
| # ) | ||||||
| # ``` | ||||||
| # | ||||||
| # In order to ensure data consistency, this method will raise a `Marten::DB::Errors::MultipleRecordsFound` | ||||||
| # exception if multiple records match the specified set of filters. | ||||||
| def update_or_create( | ||||||
| *, | ||||||
| updates : Hash | NamedTuple, | ||||||
| defaults : Hash | NamedTuple | Nil = nil, | ||||||
| **kwargs, | ||||||
| ) | ||||||
| record = get!(Node.new(**kwargs)) | ||||||
| record.set_field_values(updates) | ||||||
| record.save(using: @query.using) | ||||||
| record | ||||||
| rescue Errors::RecordNotFound | ||||||
| update_attributes = defaults.nil? ? updates : defaults.not_nil! | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we be using both the
Suggested change
|
||||||
| create do |new_record| | ||||||
| new_record.set_field_values(update_attributes) | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| # Allows to define which database alias should be used when evaluating the query set. | ||||||
| def using(db : Nil | String | Symbol) | ||||||
| qs = clone | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it could be interesting to have an
#update_or_create!variant of the method where we call#save!so that an exception is raised if the created/updated record is invalid.