I have a blog. I have Articles with Posts. Instead of destroying, I want to base on discarding everything.
I expect that you need to archive all posts before archiving the article. When this condition is not met, I want to get an error 404 and then use it as JSON in my view.
I am currently getting error 500 with the default message: "Request failed with status code 500" and in backend only: Discard::RecordNotDiscarded (Failed to discard the record).
Here is my article model:
class Article < ApplicationRecord
include Discard::Model
has_many :comments, dependent: :destroy
validates :title, presence: true
before_discard :can_destroy?, prepend: true
def can_destroy?
if posts.kept.any?
errors.add(:base, "First you have to archive all posts in this article")
throw :abort
end
end
end