attr_accessor as boolean

Hi    I have in my user model

attr_accessor :no_password_validation_needed with_options :unless => :no_password_validation_needed do |p|     p.validates_length_of :password, :minimum => 6     p.validates_confirmation_of :password end

     And from controller I do @user.no_password_validation_needed = false @user.update_attributes(params[:user])

     And it works perfectly and now validation happens. If it is @user.no_password_validation_needed = true     Then also I get the expected result as now validation not happens

    But now when @user.no_password_validation_needed = 2 #or any other value

        Now I get unexpected result as validation not happens That is same result when true? Why this?What modification should I make?Please help

Thanks in advance Tom

That's how truth values work in ruby - anything other than nil or false is generally considered true.

Fred

Does this help?

irb puts "show" if true

show

puts "show" if false

=> nil

puts "show" if nil

=> nil

puts "show" if 2

show

puts "show" if DateTime.new

show

In ruby it'll be true unless it receives nil or false.

Kind Regards Luke

Have a look at the Ruby Facets library for their "kernel.true?" method: http://facets.rubyforge.org/apidoc/api/core/classes/Kernel.html#M000436

It only returns true for a TrueClass object.

If you don't want to install the whole library, you can write a monkey patch of your own to add this functionality.