Updating an attribute in db

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

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

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:

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.

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.

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.

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.

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs