How to remove validation using instance_eval clause in Rails?

rails validation message
rails validations
rails validate on: :create
rails controller validation
rails validation context
rails email validation
rails skip specific validation
rails validate if

I would like to enhance existing class using instance_eval. There original definition contains validation, which require presence of certain fields, ie:

class Dummy < ActiveRecord::Base
  validates :field, :presence => true 
end

Now I want to change that to optional using instance_eval (or any other method, really):

Dummy.instance_eval do
  ...
end

What would be the proper syntax to remove the validation, so the field is optional. I would rather do this directly on the model layer, instead doing weird hacks in controllers or views. The use of instance_eval is not really required, but as far as I know, this is generally the best way to enhance classes in Rails.

Edit #1

In general - the original class is part of the gem and I don't want to fork it, nor tie to specific release. The general cause is not really important. Simply editing the original model has far worse consequences than monkey patching.


I found a solution, not sure how solid it is, but it works well in my case. @aVenger was actually close with his answer. It's just that the _validators accessor contains only information used for reflection, but not the actual validator callbacks! They are contained in the _validate_callbacks accessor, not to be confused with _validations_callbacks.

Dummy.class_eval do
  _validators.reject!{ |key, _| key == :field }

  _validate_callbacks.reject! do |callback|
    callback.raw_filter.attributes == [:field]
  end
end

This will remove all validators for :field. If you want to be more precise, you can reject the specific validator for _validators which is the same as the raw_filter accessor of validate callbacks.

Active Record Validations and Callbacks, I would like to enhance existing class using instance_eval. There original definition contains validation, which require presence of certain fields,  How to raise validation errors from Rails Controller? Ask Question Asked 6 years, 5 months ago. How to remove validation using instance_eval clause in Rails? 5.


I think this is the most actual solution at this moment (I'm using rails 4.1.6):

# Common ninja
class Ninja < ActiveRecord::Base
  validates :name, :martial_art, presence: true
end

# Wow! He has no martial skills
Ninja.class_eval do
  _validators[:martial_art]
    .find { |v| v.is_a? ActiveRecord::Validations::PresenceValidator }
    .attributes
    .delete(:martial_art)
end

User 18augst, Creating Custom Validation Methods; Working with Validation Errors. errors.​add_to_base; errors.add; errors.on; errors.clear; errors.size. Displaying Validation  Dummy.instance_eval do end What would be the proper syntax to remove the validation, so the field is optional. I would rather do this directly on the model layer, instead doing weird hacks in controllers or views. The use of instance_eval is not really required, but as far as I know, this is generally the best way to enhance classes in Rails.


Easest way to remove all validations:

clear_validators!

User 18augst, 11 How to remove validation using instance_eval clause in Rails? 8 How do you test if a div has a certain css style in rspec/capybara? View more network posts  Removing validation of a model declared on a gem. GitHub Gist: instantly share code, notes, and snippets.


As I was trying to do this to remove the phone validation from the spree Address model, below is the code I got to work. I added the type check for callback.raw_filter because I only wanted to remove the presence validator on the phone field. I also had to add it because it would fail when trying to run against one of the other validators specified in the Spree::Address model that did not have an 'attributes' key for callback.raw_filter, thus an exception was thrown.

Spree::Address.class_eval do

   # Remove the requirement on :phone being present.
  _validators.reject!{ |key, _| key == :phone }

  _validate_callbacks.each do |callback|
    callback.raw_filter.attributes.delete :phone if callback.raw_filter.is_a?(ActiveModel::Validations::PresenceValidator)
  end

end

Removing validation of a model declared on a gem · GitHub, 10 How to remove validation using instance_eval clause in Rails? 7 How do you test if a div has a certain css style in rspec/capybara? View more network posts  11 How to remove validation using instance_eval clause in Rails? Nov 17 '14. 3 How to override a rails generator template in a gem? Aug 27 '14.


I had a similar problem and was able to get past it using:

class MyModel << Dummy
  # erase the validations defined in the plugin/gem because they interfere with our own
  Dummy.reset_callbacks(:validate)
  ...
end

This is under Rails 3.0. The caveat: It does remove ALL validations, so if there are others you want to keep you could try Dummy.skip_callback(...), but I could not figure out the right incantation of arguments to make that work.

The Ultimate Intermediate Ruby on Rails Tutorial: Let's Create an , If you want to use Vagrant too, this is the tutorial which I found useful. before we use them. To accomplish it, remove those two lines from the application.scss file: git add -A git commit -m "Add validations to the Post model" The instance_eval method is used to test methods within a private scope. 38 How to remove validation using instance_eval clause in Rails? Sep 25 '11. 37 Rails 3 19 Rails - Populate test database with development data Apr 15 '10.


Build a RESTful JSON API With Rails 5, Rails makes it easy to build JSON APIs. Our user model defines a 1:m relationship with the todo model also adds field validations. While we're here let's also include RequestSpecHelper to all types (not just requests); remove type​: end it "raises MissingToken error" do expect { subject.instance_eval  By using our site, tosh. Apparently, this user prefers to keep an air of mystery about them. 11 How to remove validation using instance_eval clause in Rails?


ruby-on-rails, #no need of instance_eval just open the class class Dummy < ActiveRecord::​Base Per fare ciò, richiedere il file in config / application.rb dopo richiedono "​rails / all", the validations defined in the plugin/gem because they interfere with our own callback.raw_filter.attributes.delete :field end _validate_callbacks.​reject! do  By using our site, 9 How to remove validation using instance_eval clause in Rails? 1 Rails 4 lambda route request.session not yet loaded Sep 12 '13.


Validation Rails Message Field Under, Ruby on Rails Guides: Active Record Validations and Callbacks rails validation order How to remove validation using instance_eval clause in Rails? Evaluates a string containing Ruby source code, or the given block, within the context of the receiver ( obj ). In order to set the context, the variable self is set to obj while the code is executing, giving the code access to obj’s instance variables. In the version of instance_eval that takes a String,