Skip to content

Commit b5390f9

Browse files
committed
reload the created snapshot before returing in create_snapshot! method 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.
1 parent 67308c8 commit b5390f9

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

lib/active_snapshot/models/concerns/snapshots_concern.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def create_snapshot!(identifier: nil, user: nil, metadata: nil)
3434

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

37-
snapshot
37+
snapshot.reload
3838
end
3939

4040
class_methods do

test/models/snapshots_concern_test.rb

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def test_create_snapshot!
3737

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

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

0 commit comments

Comments
 (0)