Skip to content

Commit

Permalink
Updated upsert guides with extra methods for created and updated chec…
Browse files Browse the repository at this point in the history
…king. Fixes #1225 (#1360)
  • Loading branch information
jwoertink authored Aug 4, 2024
1 parent 1c77e89 commit 27d0c79
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/actions/guides/database/saving_records.cr
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,23 @@ class Guides::Database::SavingRecords < GuideAction
end
```
### Detecting updated or created
Since an `upsert` will attempt to find first and then update when found, you may want to know if the operation
performed a create or an update. You can use `created?` or `updated?` on the operation to get this value.
```crystal
SaveUser.upsert(name: "Will", email: "[email protected]") do |operation, user|
operation.created? # => true
operation.updated? # => false
end
SaveUser.upsert(name: "William", email: "[email protected]") do |operation, user|
operation.created? # => false
operation.updated? # => true
end
```
#{permalink(ANCHOR_SAVING_SERIALIZED_JSON)}
## Saving Serialized JSON
Expand Down

0 comments on commit 27d0c79

Please sign in to comment.