active_record_upsert
active_record_upsert copied to clipboard
Upsert for Rails 5 / Active Record 5
In ActiveRecord both `Model.create` and `Model.update` [always return the record](https://github.com/rails/rails/blob/master/activerecord/lib/active_record/persistence.rb#L100-L117) (or a collection of records). Unfortunately `Model.upsert` only returns the record when validations pass. When they fail [it returns `false`](https://github.com/jesjos/active_record_upsert/blob/07bdabf585405143e02bfc7306c2df09b067f847/lib/active_record_upsert/active_record/persistence.rb#L53-L57)....
You probably _can_ use the built-in Rails methods `upsert_all` and `upsert` in Rails 6. - https://medium.com/@retrorubies/upcoming-rails-6-bulk-insert-upsert-feature-2d642419557d - https://github.com/rails/rails/pull/35631 Todo: - Link to https://medium.com/@retrorubies/upcoming-rails-6-bulk-insert-upsert-feature-2d642419557d and https://github.com/rails/rails/pull/35631 prominently in the docs
Code: ```ruby class WebSession < ActiveRecord::Base self.primary_key = :id upsert_keys [:tenant_id, :session_id] belongs_to :tenant end WebSession.upsert!(tenant: tenant_id, session_id: session_id, value: {}) ``` SQL generated: ```sql INSERT INTO "web_sessions" ("tenant_id", "session_id",...
This is my migration file. ``` def change add_index :book_owners, [:book_id, :book_detail_id], unique: true, where: 'book_id IS NOT NULL and book_detail_id IS NOT NULL' end ``` This is my code....
Latest rubygems.org version is 0.9.5 which doesn't contain the opts: API described in README of this repo, which is confusing. Can latest state be released to rubygems as well?
This PR adds `exclude:` to `upsert_options` to allow one to leave certain attributes alone during an upsert. The use case is when a column is defined as ```NOT NULL DEFAULT...
When upserting a record in a database column is not nullable and has a default value, ActiveRecord::Upsert attemps to insert a nil value. This raises an exception: PG::NotNullViolation: ERROR: null...
We found that when 1) having a default in the database (here for a table called ``hardwares``) ┌─────────┬─────────────────┬──────── │ Column │ Type │ Default ├─────────┼─────────────────┼───────── │ id │ integer │...
After upserting, the very next save on the resulting record does not persist ```ruby MyModel.find_by(uuid: 1234) # nil record = MyModel.new # (id, uuid, name, age) record.uuid = 1234 #...
Hi, We are working on doing bulk data import and are looking for a library supporting upsert. Trying to understand how we can: 1. Upsert bulk array of hashes 1....