0

i have followin the http://ruby.railstutorial.org/

class User < ActiveRecord
  attr_accessible ..., :password, :password_confirmation
  has_secure_password

  validates :password, :presence => true,
                       :length => { :minimum => 6 }
  validates :password_confirmation, :presence => true

  ....
end

the problem is to create a new user this works fine, both passwords must be present and they need to match, when i update it requires me to provide the password

for example if another controller wants to change any field of User i must provide a password because otherwise i will not be able to update.

how can i formulate a contition to only require password/password_confirmation when creating the model or doing password update ?

2
  • Don't show the password and password confirmation fields on the edit form. Commented Nov 21, 2012 at 18:14
  • the problem i want change fields in another controller with no edit form, for example in the console user1 = User.first user1.field1 = "hi" user1.save this would leave a error password / password_confirmation cant be blank Commented Nov 21, 2012 at 18:17

2 Answers 2

1

If you're using rails3, you can skip validations. From the docs:

The following methods skip validations, and will save the object to the database regardless of its validity. They should be used with caution.

  • decrement!
  • decrement_counter
  • increment!
  • increment_counter
  • toggle!
  • touch
  • update_all
  • update_attribute
  • update_column
  • update_counters

Note that save also has the ability to skip validations if passed :validate => false as argument. This technique should be used with caution.

Basically, use find to find the appropriate user, update whatever fields you want, user.save!(:validate=>false), and Bob's your uncle!

Sign up to request clarification or add additional context in comments.

4 Comments

Why would you want to skip validations on user input? Doesn't that defeat the purpose of having validations to begin with?
@ChrisPeters you skip validations if, like the OP, you are updating the model and don't want to bother with the validations.
"Not bothering with validations" is the lazy way out and can cause data integrity issues. What if the user decides to leave a required field blank? They'll either generate invalid data or cause a SQL error, depending on whether or not NOT NULL constraints are placed on the underlying column.
@ChrisPeters you know what you should do then? Vote down the question, not my answer to it. OP specifically said how can i formulate a contition to only require password/password_confirmation when creating the model or doing password update? In other words, he wants validations disabled on create and update.
1

Rails supports conditional validations, e.g. in your User model

validates :password_confirmation, :presence => true, :if => :new_password

def new_password
  current_user and current_user.changing_password?
end

You would need to figure out in the new_password method how to tell whatever conditions are true when you want to validate.

See: http://railscasts.com/episodes/41-conditional-validations

Comments

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.