Cannot skip validation in Rails 3?

23,525

Solution 1

*sigh...*

Found the problem... one of my after_validate method calls was adding information and resaving the model, hence the errors I was getting weren't from the console input, they were coming from the after_validate method which was saving again.

Thanks all.

Solution 2

It is a bad practice to have invalid models saved by normal use cases. Use conditional validations instead:

validates_presence_of :title, :unless => :in_first_stage?

or if you have many:

with_options :unless => :in_first_stage? do
  validates_presence_of :title
  validates_presence_of :author
end

This way nothing stands in way to have nightly integrity tests, which checks all records for validity.

A valid use case for saving without validations would be for testing edge cases, e.g. to test that a database constraint is enforced.

Solution 3

For emergencies only

Assuming that you have considered this very carefully and are certain that this is a good idea, you can save without validation using:

my_model.save validate: false

There are almost no valid use cases for this, and it should be considered an emergency one off procedure. Your use case does not qualify.

Problems with invalid records

Having invalid records in the database leads to all manner of problems down the line. For example, you send an email to all users and update a 'last_contacted_at' field on your user model. Your invalid users will not be updated and will descend into an email spiral of death.

Conditional validation

As other posters have pointed out, conditional validation will solve most issues for which you might otherwise have used validate: false.

Solution 4

Instead of placing an invalid model in the database, store the partially completed model (created with Model.new) in a session. Only save it to the database when it is completely valid.

Share:
23,525
Andrew
Author by

Andrew

Polyglot engineer and people leader.

Updated on July 09, 2022

Comments

  • Andrew
    Andrew almost 2 years

    I'm working on a project in Rails 3 where I need to create an empty record, save it to the database without validation (because it's empty), and then allow the users to edit this record in order to complete it, and validate from then on out.

    Now I've run into a pretty basic problem: I can't seem to save a model without validating it under any circumstances.

    I've tried the following in the console:

    model = Model.new
    model.save(false) # Returns RuntimeError: Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
    model.save( :validate => false ) # Returns same error as above
    
    model = Model.create
    model.save(false) # Same runtime error
    model.save( :validate => false ) # Same runtime error
    

    I then tried changing all the validations in the model to :on => :update. Same error messages on any attempt to save.

    So what am I missing here? How can I create an empty record and then let validation occur as the user edits it?

    Thanks!

  • Andrew
    Andrew over 13 years
    That would be nice, but I can't. I have file attachments etc. that have to be submitted as part of the model, and for those to save correctly during the creation process they have to already know what the ID of the parent object is going to be.
  • thekingoftruth
    thekingoftruth almost 12 years
    The problem with doing it this way is that if the user leaves and comes back after clearing their session, that record will no longer be available. (For example, in a first time login experience).