hyper-mesh icon indicating copy to clipboard operation
hyper-mesh copied to clipboard

When create fails the object isn't destroyed

Open sfcgeorge opened this issue 7 years ago • 0 comments

When an anonymous create fails the object isn't destroyed, so the next time you create a record it actually tries to create two. I noticed this as I had a maximum validation.

If this fails due to a server side validation:

Foo.create(bar: 42)

Then when you do the same again:

Foo.create(bar: 42)

You will see from server logs / SQL / Hyperloop console that it tries to create both. They have different IDs so it's not the same object.

My workaround is this:

foo = Foo.new(bar: 42)
# fires on success or failure
foo.save.then do
  # ensure the record hasn't been saved
  # otherwise it would do a remote destroy
  foo.destroy if record.errors.any?
end

sfcgeorge avatar Jan 16 '18 22:01 sfcgeorge