krunal shah
krunal shah

Reputation: 16339

Why update_attribute firing validation?

I have two disabled textbox. And it's passing nil as value.

Still update_attribute is firing validation for those textboxes.

As per my knowledge update_attribute should not update the spefic column which has nil value passed.

validates_inclusion_of :text_response_time, :in => 0..60, :message => " should be between 0 to 60 minutes."

(rdb:589) params[:test][:text_response_time]
nil
(rdb:589) @test.update_attributes!(params[:test])
ActiveRecord::RecordInvalid Exception: Validation failed: Text response time  should be between 0 to 60 minutes.

Any Idea!

Upvotes: 0

Views: 1205

Answers (2)

Unixmonkey
Unixmonkey

Reputation: 18784

To further illustrate:

@test.update_attribute(:test, params[:test][:text_response_time])

will save without validation.

Another strategy that may work in your situation is to only validate on creation (and not updates):

validates_inclusion_of :text_response_time, :in => 0..60,
  :message => "should be between 0 to 60 minutes.", :on => :create

Upvotes: 0

Will Ayd
Will Ayd

Reputation: 7164

update attributes always validates the entire object, hence why you keep getting the failed validation. your options would then be to either simply use update_attribute (which ignores validation) or use conditional validation as described in this railscast

Upvotes: 1

Related Questions