Return to Snippet

Revision: 3404
at July 20, 2007 04:28 by bordalix


Initial Code
class User < ActiveRecord::Base
  validates_presence_of :password, :if => :should_validate_password?
  validates_presence_of :state, :on => :create
  validates_presence_of :state, :if => :in_us?

  attr_accessor :updating_password

  def in_us?
    country == 'US'
  end

  def should_validate_password?
    updating_password || new_record?
  end
end

# in controller

@user.updating_password = true
@user.save

@user.save(false)  # will avoid any validation in the model

Initial URL

                                

Initial Description
From railscast #41

Initial Title
Conditional validations

Initial Tags
rails, validation

Initial Language
Rails