Sunday, May 1, 2011

Rails: How to prevent 2 active-record attributes to be equal with validation?


I want to prevent users from signing up with a password = login for security reasons. I tried something like this:

  validates_each :password do |record, attr, value|
    if(value == self.login)

But I always get the following error: undefined method login for self. It has something to do with the class hierachy I guess, but how do I access a higher level.

I'm a little stuck here, please help.

From stackoverflow
  • Try

    if value == record.login

    the record is passed into the block as the record local variable, and in this context is not self.

    ole_berlin : Very simple... Very nice, thanks!


Post a Comment