Skip to content

Instantly share code, notes, and snippets.

@rpanachi
Last active August 29, 2015 14:09
Show Gist options
  • Save rpanachi/c911f39606752d0be33d to your computer and use it in GitHub Desktop.
Save rpanachi/c911f39606752d0be33d to your computer and use it in GitHub Desktop.
Skip Validation
# source: http://gistflow.com/posts/749-canceling-validations-in-activerecord
# declaration
def skipping_validation(methods, &block)
skipped_validations = methods.map do |method, kinds|
Array(kinds).map do |kind|
_validators[method].select { |v| v.kind == kind }.map do |validation|
_validate_callbacks.select { |c| c.filter == validation }.map do |callback|
_validate_callbacks.delete(callback)
end
end
end
end
begin
block.call
ensure
skipped_validations.flatten.each do |callback|
_validate_callbacks.append(callback)
end
end
end
# usage
skipping_validation(phone: :presence) do
self.save
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment