How can I update particular fields of model with validation in Ruby on Rails? -
there acviterecord model named user
this:
class user < activerecord::base validates :name, :presence => true validates :email, :presence => true, :uniqueness => true validates :plain_password, :presence => true, :confirmation => true validates :plain_password_confirmation, :presence => true #...other codes end
it requires update of name , email , update of password separated.
when update name , password, using update
or update_attributes
cause password validation not needed. using update_attribute
save name , email without validation.
are there ways update particular fields of model validation without causing other fields' validation?
give try, might help
class user < activerecord::base validates :name, presence: true validates :email, presence: true, :uniqueness => true validates :plain_password, length: { in: 4..255, allow_nil: true }, confirmation: true validates :plain_password_confirmation, presence: true, if: -> (user){ user.plain_password.present? } # ...... # ...... end
apart should reconsider saving plain_password ;)
Comments
Post a Comment