Answer the question
In order to leave comments, you need to log in
How to unsave an object in Rails?
Hello!
There is such a situation:
class CheckerResult < ApplicationRecord
has_many :checker_errors, dependent: :destroy
###
end
class CheckerError < ApplicationRecord
belongs_to :checker_result
###
end
checker_result = CheckerResult.new(...)
# Создается несколько таких checker_error
checker_result.checker_errors.new(...)
checker_result.checker_errors.new(...)
checker_result.save
class CheckerError < ApplicationRecord
belongs_to :checker_result
###
def delete_if_some
отменить_создание_себя if условие
end
end
before_create do
throw(:abort) if условие
end
before_create do
destroy if условие
end
Answer the question
In order to leave comments, you need to log in
We wrap the code in a transaction to be able to undo the changes.
ActiveRecord::Base.transaction do
checker_result.save
end
class CheckerError < ApplicationRecord
belongs_to :checker_result
def delete_if_some
raise ActiveRecord::Rollback
end
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question