Skip to content

Commit

Permalink
reload the created snapshot before returing in create_snapshot! metho…
Browse files Browse the repository at this point in the history
…d to prevent returing an invalid record

SnapshotItem has an uniqueness validation on `item_id`. This causes the
snapshot to be invalid after all SnapshotItems have been saved to the
database via `upsert_all`.
All records instanciated with `Snapshot#build_snapshot_item` are not
reloaded.
  • Loading branch information
henningthies committed Nov 7, 2024
1 parent 67308c8 commit b5390f9
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/active_snapshot/models/concerns/snapshots_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def create_snapshot!(identifier: nil, user: nil, metadata: nil)

SnapshotItem.upsert_all(new_entries.map{|x| x.delete("id"); x }, returning: false)

snapshot
snapshot.reload
end

class_methods do
Expand Down
1 change: 1 addition & 0 deletions test/models/snapshots_concern_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def test_create_snapshot!

snapshot = @post.create_snapshot!(identifier: "foobar 1", user: @user, metadata: {foo: :bar})
assert_not snapshot.new_record?
assert snapshot.valid?

snapshot = @post.create_snapshot!(identifier: "foobar 2", user: @user)
assert_not snapshot.new_record?
Expand Down

0 comments on commit b5390f9

Please sign in to comment.