Note that there are some explanatory texts on larger screens.

plurals
  1. POwith_options not working in RoR3 model
    text
    copied!<p>Can someone please explain to me why it seems to ignore the with_options block in this code?</p> <pre><code>class User &lt; ActiveRecord::Base attr_accessible :name,:user_type,:policy_num,:address,:city,:state,:zip,:phone,:fax,:email, :password, :password_confirmation has_secure_password has_many :policies has_many :dealer_forms before_save {|user| user.email = email.downcase} before_save :create_remember_token ZIP_REGEX = /^\d{5}(-\d{4})?$/ EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i COMPLEX_PHONE_REGEX = /^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$/ validates :name, presence: true, length:{in:4..50} validates :email, presence: true, uniqueness: {case_sensitive: false},format:{with:EMAIL_REGEX} validates :password, length:{in:6..20}, if: :password validates :password_confirmation, presence:true, if: :password validates :user_type, presence: true, inclusion: {in: %w(Dealer Producer System)} # Validate True User items skip admin users # with_options :unless =&gt; :is_system_user? do |u| u.validates :policy_num, presence: true u.validates :address, presence: true u.validates :city, presence: true u.validates :state, presence: true, length:{is: 2} u.validates :zip, presence: true, format:{with:ZIP_REGEX,message: "Not a valid Zip Code"} u.validates :phone, allow_blank: true, format:{with:COMPLEX_PHONE_REGEX} u.validates :fax, allow_blank: true, format:{with:COMPLEX_PHONE_REGEX} end private def create_remember_token self.remember_token = SecureRandom.urlsafe_base64 end def is_system_user? user_type == "System" ? true : false end end </code></pre> <p>I have tried the following:</p> <pre><code>with_options unless: :is_system_user? with_options unless: Proc.new{|user| user.user_type == "System"} with_options unless:"user_type = 'System'" with_options if: "user_type != 'System'" </code></pre> <p>but for some reason when I try and create a user in the console it still runs the validations inside the block.</p> <p>Any help would be greatly appreciated. Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload