# Update\_attribute does not validate?

**URL:** <https://www.ruby-forum.com/t/update-attribute-does-not-validate/70585>\
**Category:** Rails\
**Created:** [September 30, 2006, 3:12pm UTC](https://www.ruby-forum.com/t/update-attribute-does-not-validate/70585 "2006-09-30T15:12:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [September 30, 2006, 3:12pm UTC](https://www.ruby-forum.com/t/update-attribute-does-not-validate/70585/1 "2006-09-30T15:12:20Z")

</div>

While using the in place editor, I noticed that none of my validations  
were  
firing. Digging around in the docs I found that this is deliberate.

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000909)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.

This method is overwritten by the Validation module that’ll make sure  
that

> updates made with this method doesn’t get subjected to validation checks.  
> Hence, attributes can be updated even if the full object isn’t valid.

In the validations module,

alias\_method\_chain :update\_attribute, :validation\_skipping

# Updates a single attribute and saves the record without going

through the normal validation procedure.  
# This is especially useful for boolean flags on existing records.  
The regular +update\_attribute+ method  
# in Base is replaced with this when the validations module is  
mixed in, which it is by default.  
def update\_attribute\_with\_validation\_skipping(name, value)  
send(name.to\_s + ‘=’, value)  
save(false)  
end

Why would validations be turned off by default when using in place  
editors?  
and also why would update\_attribute have validation turned off by  
default  
with no apparent way of turning it back on?

Can anyone explain this to me please.

Cheers

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [October 2, 2006, 7:06am UTC](https://www.ruby-forum.com/t/update-attribute-does-not-validate/70585/2 "2006-10-02T07:06:10Z")

</div>

Sorry to cross post from Rails ML but I did not get any response, and  
this  
is really bugging me…

---------- Forwarded message ----------  
From: Daniel N [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: Sep 30, 2006 11:08 PM  
Subject: update\_attribute does not validate?  
To: removed\_email\_address@domain.invalid

While using the in place editor, I noticed that none of my validations  
were  
firing. Digging around in the docs I found that this is deliberate.

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000909)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.

This method is overwritten by the Validation module that’ll make sure  
that

> updates made with this method doesn’t get subjected to validation checks.  
> Hence, attributes can be updated even if the full object isn’t valid.

In the validations module,

alias\_method\_chain :update\_attribute, :validation\_skipping

# Updates a single attribute and saves the record without going

through the normal validation procedure.  
# This is especially useful for boolean flags on existing records.  
The regular +update\_attribute+ method

```
# in Base is replaced with this when the validations module is

```

mixed in, which it is by default.  
def update\_attribute\_with\_validation\_skipping(name, value)  
send(name.to\_s + ‘=’, value)  
save(false)

```
end

```

Why would validations be turned off by default when using in place  
editors?  
and also why would update\_attribute have validation turned off by  
default  
with no apparent way of turning it back on?

Can anyone explain this to me please.

Cheers

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [October 2, 2006, 7:24am UTC](https://www.ruby-forum.com/t/update-attribute-does-not-validate/70585/3 "2006-10-02T07:24:11Z")

</div>

I would guess it’s that way because inplace editing should accept  
anything you give it and deal with the input as is. How would you  
report back a failed validation? An alert?

I’m guessing you probably don’t want inplace editing for the text  
you’re trying to modify and if you do, you should update your logic to  
convert any bad data into something that will work for you. An  
example being an inplace edit where you input html when it’s not  
allowed, and you simply remove the “invalid” html on the server side  
to make it valid.

Anyway, that’s my guess.

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [October 2, 2006, 7:52am UTC](https://www.ruby-forum.com/t/update-attribute-does-not-validate/70585/4 "2006-10-02T07:52:14Z")

</div>

> I guess I could also overwrite update\_attribute to use validataion, but why  
> was validation removed from this method in the first place? I don’t want to  
> go breaking things…

Most of the other core guys are in bed right now, but I’ve never used  
the in place editor, and I share your surprise that it deliberately  
avoids validation. Perhaps David, Tobi, Thomas or Rick could chime  
in? On the other hand, it’s been deprecated in trunk, so perhaps  
this is a good opportunity for someone to extract it to a plugin and  
make it much more robust and feature filled?

–  
Cheers

Koz

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [October 2, 2006, 7:34am UTC](https://www.ruby-forum.com/t/update-attribute-does-not-validate/70585/5 "2006-10-02T07:34:13Z")

</div>

On 10/2/06, Andrew K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> allowed, and you simply remove the “invalid” html on the server side  
> to make it valid.
> 
> Anyway, that’s my guess.

Thanx for the feedback.

In general though you don’t write much when using an in\_place\*\*. You  
use an  
in\_place\_edit\_for in the controller to setup the methods, and use  
in\_place\_editor\_field in the view.

I think more importantly than reporting back that a validation failed, (  
at  
the moment until I work out something) is the fact that there is no  
validation performed in the first place.

I could manually declare the methods in the controller but I don’t want  
to  
have to declare validation logic in my in\_place editor methods  
(view/controller centric). My model should look after it’s own data.

I guess I could also overwrite update\_attribute to use validataion, but  
why  
was validation removed from this method in the first place? I don’t  
want to  
go breaking things…

Cheers

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [October 2, 2006, 7:58am UTC](https://www.ruby-forum.com/t/update-attribute-does-not-validate/70585/6 "2006-10-02T07:58:15Z")

</div>

On 10/2/06, Michael K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> avoids validation. Perhaps David, Tobi, Thomas or Rick could chime  
> in? On the other hand, it’s been deprecated in trunk, so perhaps  
> this is a good opportunity for someone to extract it to a plugin and  
> make it much more robust and feature filled?

Thanx for the feedback… I didn’t realise it was depricated. That  
still  
leaves the issue of update\_attribute though. I guess I’ll wait for  
other  
guys. Thanx again for the feedback.

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [October 2, 2006, 8:10am UTC](https://www.ruby-forum.com/t/update-attribute-does-not-validate/70585/7 "2006-10-02T08:10:16Z")

</div>

It will be a plugin eventually, so you can still use it as is, but  
you’ll have to install a plugin when a later rails version is  
released.

The case you’re speaking of is partially one reason why it will be a  
plugin; too many people want it to do different things and when that  
happens the functionality should remain as a plugin for a while (if  
not forever).

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [October 9, 2006, 3:48am UTC](https://www.ruby-forum.com/t/update-attribute-does-not-validate/70585/8 "2006-10-09T03:48:44Z")

</div>

Does anyone have any thoughts as to why the update\_attribute method does  
not  
validate, and has no way to turn validation on?

Cheers

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [October 9, 2006, 8:09am UTC](https://www.ruby-forum.com/t/update-attribute-does-not-validate/70585/9 "2006-10-09T08:09:07Z")

</div>

I think having this method NOT validate is useful for the handful of  
situations in which you want to update the underlying database even  
though the valid object hasn’t been fully saved.

Eg,  
model = Model.find(42)

# do something invalid

model.update\_attribute(:invalid, true) unless model.save

I’ve never run into a situation where I’ve needed this, but it’s nice  
to know it’s there. You may want to use Ruby’s class rewriting  
capabilities to update the in\_place\_edit\_for method to use  
#update\_attributes (which will do validation checks):

module ActionController  
module Macros  
module InPlaceEditing  
module ClassMethods  
def in\_place\_edit\_for(object, attribute, options = {})  
define\_method(“set\_#{object}\_#{attribute}”) do  
@item = object.to\_s.camelize.constantize.find(params[:id])  
@item.update\_attributes({ attribute =\> params[:value] })  
render :text =\> @item.send(attribute)  
end  
end  
end  
end  
end  
end

Or, you can just write out your own set\_\* methods.
