# Updating an attribute in db

**URL:** <https://www.ruby-forum.com/t/updating-an-attribute-in-db/81598>\
**Category:** Rails\
**Created:** [January 10, 2007, 1:03pm UTC](https://www.ruby-forum.com/t/updating-an-attribute-in-db/81598 "2007-01-10T13:03:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ptx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@Ptx](https://www.ruby-forum.com/u/Ptx)\
**Post date:** [January 10, 2007, 1:03pm UTC](https://www.ruby-forum.com/t/updating-an-attribute-in-db/81598/1 "2007-01-10T13:03:33Z")

</div>

Hello

I am a Ruby newbie, thus a rather simple question.  
My list view shows table records and enables user to click a button in  
order to change value of one attribute in given record. I use this code  
to invoke a method from controller:

\<%= button\_to “Change”, { :action =\> “edit”, :id =\> prog.id\_prog } %\>

My method looks like this (just a simple one - to set the salary  
attribute to 1000)

def edit  
prog = Prog.find(params[:id])  
prog.update\_attribute(:salary, 1000)  
redirect\_to :action =\> ‘list’  
end

However after a list view is shown again the change is not made to the  
database. I do not get it, as find returns the proper record and  
update\_attribute returns true. What may be wrong?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Ptx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@Ptx](https://www.ruby-forum.com/u/Ptx)\
**Post date:** [January 10, 2007, 5:19pm UTC](https://www.ruby-forum.com/t/updating-an-attribute-in-db/81598/2 "2007-01-10T17:19:33Z")

</div>

Ptx wrote:

> attribute to 1000)
> 
> def edit  
> prog = Prog.find(params[:id])  
> prog.update\_attribute(:salary, 1000)

> ```
> redirect_to :action => 'list'
> 
> ```
> 
> end
> 
> However after a list view is shown again the change is not made to the  
> database. I do not get it, as find returns the proper record and  
> update\_attribute returns true. What may be wrong?
> 
> Thanks in advance

I believe you need a ‘prog.save’ in there, before the redirect\_to

---

<div class="post-metadata">

**Author:** ![Ptx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@Ptx](https://www.ruby-forum.com/u/Ptx)\
**Post date:** [January 10, 2007, 5:22pm UTC](https://www.ruby-forum.com/t/updating-an-attribute-in-db/81598/3 "2007-01-10T17:22:36Z")

</div>

On Jan 10, 2007, at 11:17 AM, Benjamin Ritcey wrote:

> > \<%= button\_to “Change”, { :action =\> “edit”, :id =\> prog.id\_prog } %\>
> > 
> > However after a list view is shown again the change is not made to  
> > the  
> > database. I do not get it, as find returns the proper record and  
> > update\_attribute returns true. What may be wrong?
> > 
> > Thanks in advance
> 
> I believe you need a ‘prog.save’ in there, before the redirect\_to

No, update\_attribute automatically saves the record:

> **[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.

Does the code work when executed from the console?

One thing you might like to try is logging an inspection of the prog  
object to see if that gives any insight:

def edit  
prog = Prog.find(params[:id])  
prog.update\_attribute(:salary, 1000)

```
 # Something like:
 logger.warn prog.inspect

 redirect_to :action => 'list'

```

end

James.

---

<div class="post-metadata">

**Author:** ![Ptx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@Ptx](https://www.ruby-forum.com/u/Ptx)\
**Post date:** [January 10, 2007, 5:26pm UTC](https://www.ruby-forum.com/t/updating-an-attribute-in-db/81598/4 "2007-01-10T17:26:45Z")

</div>

update\_attribute() calls save(), but remember that save() can return  
false on failure.

Test the return value of prog.update\_attribute(:salary, 1000). You’ll  
probably find that it’s false, which means the save() failed.

Then you can start looking for _why_ it failed. However, this is a  
good lesson. _Always_ test for save failure in actions that call it.

---

<div class="post-metadata">

**Author:** ![Ptx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@Ptx](https://www.ruby-forum.com/u/Ptx)\
**Post date:** [January 10, 2007, 5:35pm UTC](https://www.ruby-forum.com/t/updating-an-attribute-in-db/81598/5 "2007-01-10T17:35:49Z")

</div>

Sheldon H. wrote:

> Test the return value of prog.update\_attribute(:salary, 1000). You’ll  
> probably find that it’s false, which means the save() failed.

update\_attribute returns true. I checked it already as mentioned in  
first post.

---

<div class="post-metadata">

**Author:** ![Ptx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@Ptx](https://www.ruby-forum.com/u/Ptx)\
**Post date:** [January 10, 2007, 8:16pm UTC](https://www.ruby-forum.com/t/updating-an-attribute-in-db/81598/6 "2007-01-10T20:16:20Z")

</div>

Another hint - update\_all() works fine, however it is a bit neat to  
update in that way, so if anyone has any more suggestions, I will be  
grateful.

---

<div class="post-metadata">

**Author:** ![Ptx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@Ptx](https://www.ruby-forum.com/u/Ptx)\
**Post date:** [January 10, 2007, 5:32pm UTC](https://www.ruby-forum.com/t/updating-an-attribute-in-db/81598/7 "2007-01-10T17:32:52Z")

</div>

Benjamin Ritcey wrote:

> Ptx wrote:
> 
> > def edit  
> > prog = Prog.find(params[:id])  
> > prog.update\_attribute(:salary, 1000)
> 
> > ```
> > redirect_to :action => 'list'
> > 
> > ```
> > 
> > end

> I believe you need a ‘prog.save’ in there, before the redirect\_to

This does not solve the problem. Still the update has no effect.
