Oğuz Tanrıkulu
Oğuz Tanrıkulu

Reputation: 239

Ruby on rails Update

I have User model, it has some validations and they work on create. But when i call any user from database as @user=User.find(1) @user.valid? it returns false. Could you help me?

class User < ActiveRecord::Base

  validates :name, :surname, :username,  :phone, :role, :gender, :presence => true

  validates :password_confirmation, :email_confirmation, :presence => true

  validates :username, :email, :uniqueness => true

  validates :verified, :bulletin, :inclusion => { :in => [true, false] }

  validates :password,:email, :confirmation => true

  ....

end

Upvotes: 0

Views: 38

Answers (2)

Meier
Meier

Reputation: 3880

There is a special validation for this use case, that the user should provide a confirmation, but the confirmation is not stored in the database

validates :email, confirmation: true, :uniqueness => true
validates :password, confirmation: true, ....

This substitutes the validation for :password_confirmation and :email_confirmation, so you need also to remove them.

See the fine rails guides http://guides.rubyonrails.org/active_record_validations.html#confirmation

Upvotes: 0

Olivier
Olivier

Reputation: 696

I guess you need to add on: :create param for each validations that only need to be run on create. For example when you're doing @user.valid? I gess you don't want to check if password_confirmation is present. So in this case it should be: validates :password_confirmation, :email_confirmation, :presence => true, :on => :create

Hope it helps :)

Upvotes: 1

Related Questions