ActiveRecord bug? Or am I getting it wrong? (validates_presence_of if)

Posted by Dmitriy Likhten on Stack Overflow See other posts from Stack Overflow or by Dmitriy Likhten
Published on 2010-04-30T05:56:21Z Indexed on 2010/04/30 6:07 UTC
Read the original article Hit count: 165

Filed under:
|

Ok:

User
    attr_accessible :name, :email, :email_confirmation

    validates_presence_of :email_confirmation if :email_changed?

What happens in the following situation:

u = User.find 1
u.name = 'Fonzi'
u.name_changed? # => true
u.email_changed? # => false
u.valid? # => false : email_confirmation is required

Basically, if I change if to unless the validates works as expected, won't validate if the email has not changed, will validate if the email changed. I thought the IF indicates "run this validation if the following function returns true. Seems to work backwards!? Am I just getting it wrong?

© Stack Overflow or respective owner

Related posts about activerecord

Related posts about ruby-on-rails