Skip to content
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

Document differences between this gem and ActiveRecord::Persistence#upsert #131

Merged
merged 2 commits into from
Nov 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,26 @@ Overriding the models' `upsert_options` (partial index) when calling `#upsert` o
Account.upsert(attrs, opts: { upsert_options: { where: 'foo IS NOT NULL' } })
# Or, on an instance:
account = Account.new(attrs)
account.upsert(opts: { upsert_options: { where: 'foo IS NOT NULLL } })
account.upsert(opts: { upsert_options: { where: 'foo IS NOT NULL' } })
```

## Comparing to native Rails 6 Upsert

Rails 6 (via the ["Add insert_many to ActiveRecord models" PR #35077](https://github.com/rails/rails/pull/35077)) added the ability to create or update individual records through `#insert` and `#upsert` and similarly the ability to create or update multiple records through `#insert_all` and `#upsert_all`.

Here is a quick comparison of how the Rails native `ActiveRecord::Persistence#upsert` feature compares to what's offered in this gem:

| Feature | `active_record_upsert` | Rails native `ActiveRecord::Persistence#upsert`
|--|--|--|
| Set model level conflict clause | Yes, through `#upsert_keys` | No, but can be passed in through the `:unique_by` option |
| Ability to invoke validations and callbacks | Yes | No |
| Automatically sets `created_at`/`updated_at` timestamps | Yes | Yes (Rails 7.0+) |
| Checks for unique index on the database | No[^1] | Yes |
| Use associations in upsert calls | Yes | No |
| Return object type | Instantiated ActiveRecord model | `ActiveRecord::Result` |

[^1]: Though the gem does not check for the index first, the upsert will still fail due to the database constraint.

## Tests

Make sure to have an upsert_test database:
Expand Down