Skip to content

Instantly share code, notes, and snippets.

@benshimmin
Created March 6, 2014 23:20
Show Gist options
  • Save benshimmin/9401836 to your computer and use it in GitHub Desktop.
Save benshimmin/9401836 to your computer and use it in GitHub Desktop.
How to raise a validation error in Rails after save
# This probably isn't a good thing to want to do, but it came up for me,
# so in the spirit of helping others with weird problems (and because this
# seems to be documented almost nowhere):
after_save do
if some_failing_condition
errors.add(:something, "some failure happened.")
raise ActiveRecord::RecordInvalid.new(self)
end
end
@shimaamarzouk
Copy link

Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment