class CreateStudy
def call(attributes)
study = Study.new(attributes)
ActiveRecord::Base.transaction do
begin
study.save!
raise "something bad happens here"
rescue
broadcast(:failed, study)
raise ActiveRecord::Rollback
else
broadcast(:successful, study)
broadcast(:study_created, id: study.id)
end
end
end
end
When an error occurs after study.create!
, because broadcast(:failed, study)
is inside the transaction the study
object will have an id
. Thus
to whatever is listening to the failed event will think the study has been persisted because the rollback has not yet occured.
So in a controller is you subscribe to the failed event in order to render the new form again it will have the wrong URL, since it will think it is rendering the form for a persisted model.
Note Rollback does not reset the id anyway.