Trip
Trip

Reputation: 27114

When an object returns false for `.valid?` , is there a way to find out why?

When an object returns false for .valid? , is there a way to find out why?

CardSignup.new(params[:card_signup]).valid?
=> false

Sounds great Rails..but why?

Sort of related, but here's an example. I'm doing this :

@card_signup.update_attributes("email"=>"[email protected]")
=> false

But if I do this :

@card_signup.update_attribute("email", "[email protected]")
=> true

Why would that work when I update the single attribute as opposed to update_attributes?

Upvotes: 0

Views: 297

Answers (4)

Bharath
Bharath

Reputation: 1

use current_devise_model.errors or devise_model.find(id).errors current_devise_model -> example :user, :admin this is will return #<ActiveModel::Errors []> with this you can resolve this issue mostly because of duplicated values updated or required attributes not updated

Upvotes: 0

Jef
Jef

Reputation: 5474

You should check the @card_signup.errors collection.

For your second question, the update_attribute method saves the record without validation procedure. On the opposite, update_attributes perform validations.

Upvotes: 5

Cyberfox
Cyberfox

Reputation: 1145

Single attribute updates don't go through the validation process.

If a constructed ActiveRecord object isn't valid, try accessing the errors method.

E.g.

c = CardSignup.new(params[:card_signup])
puts c.errors.to_a.inspect if !c.valid?

Upvotes: 1

tobiasbayer
tobiasbayer

Reputation: 10379

Yes, there's a method @card_signup.errors.

Upvotes: 0

Related Questions