0

It's simple enough to skip validations when seeding a specific record using save!(..., validate: false) (as outlined here and here).

I have a case where this is not ideal, because I have a db dump (using seed_dump gem), so the seeds.rb file has a lot of information and I hope to avoid making a large number of code changes, and prefer to just turn off validations temporarily (and turn them back on after seeding).

Is it possible to turn off all validations while seeding without editing every single create/save in the seeds file? (I hope to run some line before seeding to turn validations off, then again after seeding to turn validations back on again, that way only two lines require editing, not dozens, minimising code changes).

stevec
  • 41,291
  • 27
  • 223
  • 311

1 Answers1

4

You can do this by temporary overriding method valid? on ApplicationRecord class

class ApplicationRecord < ActiveRecord::Base
  primary_abstract_class

  def valid?
    true
  end
end

Or you can even do it within your seed task or in some callback before your seeds are running

ApplicationRecord.class_eval do
  def valid?
    true
  end
end
Yurii Stefaniuk
  • 1,594
  • 2
  • 11
  • 16