Rails 3: validates :presence => true vs validates_presence_of

ruby-on-rails-3

What is the difference between validates :presence and validates_presence_of? Looking through ActiveModel it looks like they setup the validation the same way. However, given the following model definition:

class Account < ActiveRecord::Base
  has_one :owner_permission, :class_name => 'AccountPermission', :conditions => { :owner => true, :admin => true }
  has_one :owner, :class_name => 'User', :through => :owner_permission, :source => :user

  validate :owner, :presence => true
  validates_associated :owner
end

Calling save on an instance of Account does not validate the presence of owner. Though, if I use validates_presence_of it will.

Best Answer

All those validates_whatever_of :attr macros do is call validates :attr, :whatever => true.

The problem is you are using validate and not validates.

Related Topic