# How do I stop a column being updated by model.save?

**URL:** <https://www.ruby-forum.com/t/how-do-i-stop-a-column-being-updated-by-model-save/104656>\
**Category:** Rails\
**Created:** [May 21, 2007, 10:19am UTC](https://www.ruby-forum.com/t/how-do-i-stop-a-column-being-updated-by-model-save/104656 "2007-05-21T10:19:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@davidf](https://www.ruby-forum.com/u/davidf)\
**Post date:** [May 21, 2007, 10:19am UTC](https://www.ruby-forum.com/t/how-do-i-stop-a-column-being-updated-by-model-save/104656/1 "2007-05-21T10:19:54Z")

</div>

One of my models has a column that is updated very frequently from a  
separate process, so it is important that when a record is saved in  
rails, this column should be left alone. In the update method of the  
controller I have:

@record = MyModel.find(params[:id])  
@record.update\_attributes(params[:my\_model])

params[:my\_model] doesn’t have a reference to the column I’m talking  
about, however, the UPDATE statement produced by update\_attributes will  
still assign a value to this column.

Even though its unlikely, if the separate process updates the record in  
the time betweent the find, and the update, then the new value will be  
lost. Is there a way to prevent this from happening?

p.s. attr\_protected doesn’t help me

---

<div class="post-metadata">

**Author:** ![davidf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@davidf](https://www.ruby-forum.com/u/davidf)\
**Post date:** [May 21, 2007, 10:42am UTC](https://www.ruby-forum.com/t/how-do-i-stop-a-column-being-updated-by-model-save/104656/2 "2007-05-21T10:42:32Z")

</div>

On 5/21/07, David F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> about, however, the UPDATE statement produced by update\_attributes will  
> still assign a value to this column.
> 
> Even though its unlikely, if the separate process updates the record in  
> the time betweent the find, and the update, then the new value will be  
> lost. Is there a way to prevent this from happening?

One option, not very clean and elegant, and probably prone to  
deadlocks if you update several rows during a transaction, is locking  
the row & reading its’ current value from a before\_save callback.

E.g.:

def before\_save  
unless self.new\_record?  
current = self.class.find(self.id, :lock =\> true) # “select for  
update”, locking row  
write\_attribute(:some\_attribute, current.some\_attribute)  
end  
end

Isak

---

<div class="post-metadata">

**Author:** ![davidf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@davidf](https://www.ruby-forum.com/u/davidf)\
**Post date:** [May 21, 2007, 11:19am UTC](https://www.ruby-forum.com/t/how-do-i-stop-a-column-being-updated-by-model-save/104656/3 "2007-05-21T11:19:22Z")

</div>

> def before\_save  
> unless self.new\_record?  
> current = self.class.find(self.id, :lock =\> true) # “select for  
> update”, locking row  
> write\_attribute(:some\_attribute, current.some\_attribute)  
> end  
> end
> 
> Isak

Its nasty, but it might work. Thanks!

---

<div class="post-metadata">

**Author:** ![davidf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@davidf](https://www.ruby-forum.com/u/davidf)\
**Post date:** [May 21, 2007, 10:51am UTC](https://www.ruby-forum.com/t/how-do-i-stop-a-column-being-updated-by-model-save/104656/4 "2007-05-21T10:51:32Z")

</div>

On May 21, 2007, at 10:19 AM, David F. wrote:

> about, however, the UPDATE statement produced by update\_attributes  
> will  
> still assign a value to this column.
> 
> Even though its unlikely, if the separate process updates the  
> record in  
> the time betweent the find, and the update, then the new value will be  
> lost. Is there a way to prevent this from happening?
> 
> p.s. attr\_protected doesn’t help me

The invocation is

attr\_readonly :foo

but according to

[http://dev.rubyonrails.org/ticket/6896](http://dev.rubyonrails.org/ticket/6896)

is scheduled for 1.2.4.

– fxn

---

<div class="post-metadata">

**Author:** ![davidf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@davidf](https://www.ruby-forum.com/u/davidf)\
**Post date:** [May 21, 2007, 12:38pm UTC](https://www.ruby-forum.com/t/how-do-i-stop-a-column-being-updated-by-model-save/104656/5 "2007-05-21T12:38:45Z")

</div>

> @record.update\_attribute(“your\_column”, params[:value])

This has the same problem, both “update\_attribute” and  
“update\_attributes” call the “save” method, which in turn, calls the  
“update” method. This is whats setting the values of all the columns in  
the table.

---

<div class="post-metadata">

**Author:** ![davidf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@davidf](https://www.ruby-forum.com/u/davidf)\
**Post date:** [May 21, 2007, 12:28pm UTC](https://www.ruby-forum.com/t/how-do-i-stop-a-column-being-updated-by-model-save/104656/6 "2007-05-21T12:28:13Z")

</div>

hi david,

have you ever try this:  
@record = MyModel.find(params[:id])  
@record.update\_attribute(“your\_column”, params[:value])

for updating several column, you could use this instead:  
@record.update\_attribute(“your\_column1”, params[:value1],  
“your\_column2”,  
params[:value2])

afaik, if you do somethink like this:  
@record.update\_attributes(params[:value])

then all your column will be updated using apt column-name with array of  
:value, so that you previous value of column will be lost regardles.

regard,

~inung;

On 21/05/07, David F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Isak
> 
> Its nasty, but it might work. Thanks!
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
in03ng a.k.a inung a.k.a nursamsi a.k.a nur syamsi  
Y! in03ng

---

<div class="post-metadata">

**Author:** ![davidf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@davidf](https://www.ruby-forum.com/u/davidf)\
**Post date:** [May 21, 2007, 1:04pm UTC](https://www.ruby-forum.com/t/how-do-i-stop-a-column-being-updated-by-model-save/104656/7 "2007-05-21T13:04:10Z")

</div>

Yes, both attr\_protected and attr\_accessible only allow/prevent values  
being assigned at the Rails level, the underlying SQL UPDATE that is  
executed is still updating the values of all the columns.

Thanks anyway for trying!

---

<div class="post-metadata">

**Author:** ![davidf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@davidf](https://www.ruby-forum.com/u/davidf)\
**Post date:** [May 21, 2007, 3:15pm UTC](https://www.ruby-forum.com/t/how-do-i-stop-a-column-being-updated-by-model-save/104656/8 "2007-05-21T15:15:58Z")

</div>

David F. wrote:

> still assign a value to this column.
> 
> Even though its unlikely, if the separate process updates the record in  
> the time betweent the find, and the update, then the new value will be  
> lost. Is there a way to prevent this from happening?
> 
> p.s. attr\_protected doesn’t help me

Check out optimistic locking.

–  
Jack C.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![davidf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@davidf](https://www.ruby-forum.com/u/davidf)\
**Post date:** [May 21, 2007, 12:59pm UTC](https://www.ruby-forum.com/t/how-do-i-stop-a-column-being-updated-by-model-save/104656/9 "2007-05-21T12:59:16Z")

</div>

sorry david, i’m wrong getting your first message. anyway, have u ever  
try  
using attr\_accessible? n00b here, just trying to help…😃

regards,

~inung;

On 21/05/07, David F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
in03ng a.k.a inung a.k.a nursamsi a.k.a nur syamsi  
Y! in03ng
