In general if we need to skip validation we can do it by :
user.save(validate: false)
validate: false skips validation from all the attributes.
But what if we need skip validation on particular field like password validation of devise gem.
In User.rb
attr_accessor :skip_password_validation
private
def password_required?
return false if skip_password_validation
super
end
In registrations_controller.rb
def configure_sign_up_params
params.require(:user).permit(:email, :password, :password_confirmation, :skip_password_validation)
end
Now, whenever you want to skip password validation, pass skip_password_validation params as true
user.skip_password_validation = true
user.save
It will create User without password.
user.save(validate: false)
validate: false skips validation from all the attributes.
But what if we need skip validation on particular field like password validation of devise gem.
In User.rb
attr_accessor :skip_password_validation
private
def password_required?
return false if skip_password_validation
super
end
In registrations_controller.rb
def configure_sign_up_params
params.require(:user).permit(:email, :password, :password_confirmation, :skip_password_validation)
end
Now, whenever you want to skip password validation, pass skip_password_validation params as true
user.skip_password_validation = true
user.save
It will create User without password.
Is a Beuty solution! thx for sharing :D
ReplyDelete