ruby on rails - How to update model attribute from Model? -
i have method change user status inside it's model, possible use inside user model:
class user < activerecord::base def confirm! super self.update_column(:status => "active") end end i saw these 2 examples;
how update attributes in self model rails
couldn't quite 1 go with!
it depends on whether or not want validations in model run. update_attribute not run validations, update_attributes will. here couple of examples.
using update_attributes:
class user < activerecord::base validates :email, presence: true def confirm! update_attributes(status: 'active') end end the following return false , not update record, because not email has been set:
user = user.new user.confirm! # returns false using update_attribute:
class user < activerecord::base validates :email, presence: true def confirm! update_attribute(:status, 'active') end end the following update status active regardless of whether or not email has been set:
user = user.new user.confirm! # returns true
Comments
Post a Comment