Turn of a single validation

During migration I would like to turn off a single validation.

That is, I don't want to turn off all validations for a single record, I want to turn of a single validation (of several) for all records.

If you're using models in a migration you should usually be creating a private copy of the class in the migration ie

class SomeMigration < ...   class SomeModel < ActiveRecord::Base   end

  def self.up     ...   end end

( see also http://guides.rubyonrails.org/migrations.html#using-models-in-your-migrations )

Fred

Frederick Cheung wrote:

If you're using models in a migration you should usually be creating a private copy of the class in the migration ie

. . .

Fred, thank you.

What you propose is the right way to do things. I'm looking for the wrong way to do things .. that is, I'm looking to turn off a validation once it has been turned on.

Ralph

I think you missed Frederick's point, (or I have missed yours). Note that he is suggesting putting a local declaration of your model class _inside_ the migration. This means that any validations in your model.rb file will not be seen. There should not be a need for validations inside a migration, so the fact that all validations are being ignored should not matter. (You are doing a migration not a seed aren't you?).

class SomeMigration < ... class SomeModel < ActiveRecord::Base end

def self.up    ... end end

Colin Law wrote:

You are doing a migration not a seed aren't you?).

I'm doing a seed with "random" data and I want all the validations ... except one.

I know. I know. I'm bad bad bad. But this is a hack AND I'd like to know if the hack can be done.

Ralph Shnelvar wrote:

Colin Law wrote:

You are doing a migration not a seed aren't you?).

I'm doing a seed with "random" data and I want all the validations ... except one.

Seeds don't belong in migrations, and anyway they shouldn't need you to turn off validations.

I know. I know. I'm bad bad bad. But this is a hack AND I'd like to

Don't hack. Don't do things you know are bad. Period.> know if the hack can be done.

Colin Law wrote: >You are doing a migration not a > seed aren't you?).

I'm doing a seed with "random" data and I want all the validations ... except one.

I know. I know. I'm bad bad bad. But this is a hack AND I'd like to know if the hack can be done.

You could probably twist the :if/:unless options for this, although it will almost certainly come and bite you later on.

Fred

Another option: If you can trust your seed data save(false) turns off validations.