Lawrence Dawson Lawrence Dawson - 1 year ago 154
Ruby Question

validates_confirmation_of :password

I have this code, that works, but I'm not really sure how?

It validates passwords but how does it do this?

I know what an attr_reader and accessor are but don't really understand how datamapper knows to compare :password with :password_confirmation? What magic is datamapper performing?

Here is my user model:

require 'data_mapper'
require 'dm-postgres-adapter'
require 'bcrypt'

class User

include BCrypt
include DataMapper::Resource

property :id, Serial
property :username, String
property :email, String
property :password_digest, Text

validates_confirmation_of :password

attr_reader :password
attr_accessor :password_confirmation

def password=(password)
@password = password
self.password_digest = BCrypt::Password.create(password)


Here is my controller post:

post '/sign-up' do
new_user = User.create(:username => params[:username], :email => params[:email], :password => params[:password], :password_confirmation => params[:password_confirmation])
session[:user_id] =
redirect '/welcome'

Answer Source

Well, here's the docs for validates_confirmation_of

The "magick" that happens is only that data-mapper will search for a field with the same name of the one you passed in the validates_confirmation_of (in this case :password with _confirmation in the end, and verify i they are equal.

For example validates_confirmation_of :email will make datamapper look for an attribute email_confirmation and compare if it's equal to :email.

attr_reader and attr_accessor

The attr_reader and attr_acessor are just 'shortcuts' to define methods and instance variable.

  • attr_reader: creates a method that gets the attribute
  • attr_writer: creates a method that sets the attribute
  • attr_accessor: creates both

For example:

class Person

   attr_reader :name
   attr_accessor :gender


Is the same thing of:

class Person

  def name

  def gender=(gender)
    @gender = gender

  def gender

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download