0

I have the following model

class User < ActiveRecord::Base
  VALID_EMAIL_REGEX = /^.+@.+\..+$/i 
  attr_accessible :active_list_id, :password, :password_confirmation, :email, :temp
  has_secure_password
  before_create { generate_token(:auth_token) }
  if :temp.nil?
    before_validation :downcase_email
    validates_presence_of :email, :password, :password_confirmation, :on => :create
    validates_confirmation_of :password
    #[email protected]
    validates :email, :uniqueness => true, 
              :format => {:with => VALID_EMAIL_REGEX }
  end
  after_create { make_list([email,"'s shopping list"].join('')) }

  has_many :shopping_lists
  has_many :transactions, :class_name => 'Order'

  HUMANIZED_ATTRIBUTES = {
    :password_digest => "Password"
  }
  ...
end

I try to create a model in my rails console by calling User.create(:temp => true) (which is a boolean and is defined in my migrations/schema). But it always rollsback the transaction. What am I doing wrong?

I also tried doing :if => temp.nil? and if => "temp.nil?" and :if => lambda { |user| user.temp.nil? } for all 3 of my validations.

1
  • turns out has_secure_password auto validates :( Commented Mar 10, 2013 at 11:03

1 Answer 1

0

create a method called temp_is_nil? and use that on the if condition

def temp_is_nil?
  temp.nil?
end

make sure that temp is an attribute of user.

before_validation :downcase_email, if: temp_is_nil?
validates_presence_of :email, :password, :password_confirmation, :on => :create, if: temp_is_nil?
validates_confirmation_of :password, if: temp_is_nil?
validates :email, :uniqueness => true, 
          :format => {:with => VALID_EMAIL_REGEX }, if: temp_is_nil?
Sign up to request clarification or add additional context in comments.

1 Comment

I figured it out. Turns out I can't skip has_secure_password validations. Here is a ~duplicate question with the solution: stackoverflow.com/questions/10442466/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.