Skip to content

Instantly share code, notes, and snippets.

@hasghari
Last active August 29, 2015 14:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hasghari/8017672dee0812797c1f to your computer and use it in GitHub Desktop.
Save hasghari/8017672dee0812797c1f to your computer and use it in GitHub Desktop.
module SaveWithErrors
def self.prepended(base)
base.serialize :record_errors, Hash
base.after_validation :assign_errors
end
def save(*args)
super(*args) || save_anyway
end
def save!(*args)
super(*args)
rescue ActiveRecord::RecordInvalid
save_anyway
raise
end
private
def assign_errors
self.record_errors = errors.messages
end
def save_anyway
save(validate: false)
false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment