# Some attributes not updated when validation fails?

**URL:** <https://www.ruby-forum.com/t/some-attributes-not-updated-when-validation-fails/69443>\
**Category:** Rails\
**Created:** [September 21, 2006, 3:06am UTC](https://www.ruby-forum.com/t/some-attributes-not-updated-when-validation-fails/69443 "2006-09-21T03:06:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [September 21, 2006, 3:06am UTC](https://www.ruby-forum.com/t/some-attributes-not-updated-when-validation-fails/69443/1 "2006-09-21T03:06:31Z")

</div>

I have what I think is a simply question.

If I call update\_attributes(params[:whatever]) to update my attributes  
for object :whatever, does the attribute updating stop as soon as any  
validation rule fails?

Assume 5 attributes, a-e. If a validates\_xxx for “c” fails, does  
update\_attributes return right away?

The reason I’m asking is that I have some attributes that I want to  
update even if validation for other attributes fails.

Do I need to take complete control of my attribute updating in this  
case?

I see that “update\_attribute” (not plural) seems to address this.

Do I need to update my attributes one-by-one using update\_attribute on  
params.each?

If so, what is the point of update\_attributes then?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [September 22, 2006, 4:55pm UTC](https://www.ruby-forum.com/t/some-attributes-not-updated-when-validation-fails/69443/2 "2006-09-22T16:55:35Z")

</div>

Wes G. wrote:

> update even if validation for other attributes fails.
> 
> Do I need to take complete control of my attribute updating in this  
> case?
> 
> I see that “update\_attribute” (not plural) seems to address this.
> 
> Do I need to update my attributes one-by-one using update\_attribute on  
> params.each?

No, update\_attributes will update all non-protected attributes in  
its argument hash before validation is performed. Is this not  
what you’re seeing?

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [September 23, 2006, 7:21pm UTC](https://www.ruby-forum.com/t/some-attributes-not-updated-when-validation-fails/69443/3 "2006-09-23T19:21:02Z")

</div>

> No, update\_attributes will update all non-protected attributes in  
> its argument hash before validation is performed. Is this not  
> what you’re seeing?

Mark,

I am seeing the expected behavior on one page but not another. I have  
convinced myself that update\_attributes works so I just need to figure  
out what other problem I have.

One thing to note is that I combined update\_attributes and save!  
together into one function that I named update\_attributes! (defined in  
environment.rb) - seen below.

class ActiveRecord::Base  
def update\_attributes!(attributes)  
self.attributes = attributes  
save!  
end  
end

This could be it, huh? I’m never actually calling update\_attributes  
here. I will test it.

Thanks for replying.

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [September 23, 2006, 7:33pm UTC](https://www.ruby-forum.com/t/some-attributes-not-updated-when-validation-fails/69443/4 "2006-09-23T19:33:59Z")

</div>

On the other hand, I’m using this exact same scheme in two places and  
things work as I expect them to in one but not the other.

I’ll figure it out and post the solution when I have it.

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [September 24, 2006, 3:07pm UTC](https://www.ruby-forum.com/t/some-attributes-not-updated-when-validation-fails/69443/5 "2006-09-24T15:07:31Z")

</div>

Wes G. wrote:

> This could be it, huh? I’m never actually calling update\_attributes  
> here. I will test it.

It’s the same way core currently does update\_attributes, so it  
should be fine, and would be a useful core addition.

Are you sure that the attributes you’re updating aren’t protected  
(including id and STI-type), and that there are no lurking  
custom attribute setter methods.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [September 25, 2006, 8:28pm UTC](https://www.ruby-forum.com/t/some-attributes-not-updated-when-validation-fails/69443/6 "2006-09-25T20:28:23Z")

</div>

Mark,

Thanks for the suggestion that my update\_attributes! method would be  
useful overall. I think so too.

As I suspected, my problem was my own. I had two forms that did the  
exact same thing, which was:

Given a checkbox, if the checkbox was checked, I wanted to force a text  
field’s attribute value to be 0. So my attribute setters would have to  
handle ensuring that text box attribute was 0 if checkbox attribute was  
‘1’.

My ultimate problem was that the naming of my fields in the two forms  
was different so that in one form, the checkbox was updated before the  
text field because those attributes were alphabetically in that order,  
and in the other, the opposite update order was occurring.

Because my setter for the text field wasn’t smart enough to check the  
state of the checkbox, in one form, the checkbox would force the  
attribute value of the text field to 0, and then the text field would  
happily update itself to be the value from the form. This led to my  
desperate question :).

In fact, I should probably force the text field value on the client _as  
soon as_ the checkbox is checked using Javascript, and then my setters  
don’t have to be so smart.

Wes
