# Strange update\_attributes behavior

**URL:** <https://www.ruby-forum.com/t/strange-update-attributes-behavior/144705>\
**Category:** Rails\
**Created:** [August 1, 2008, 11:03am UTC](https://www.ruby-forum.com/t/strange-update-attributes-behavior/144705 "2008-08-01T11:03:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [August 1, 2008, 11:03am UTC](https://www.ruby-forum.com/t/strange-update-attributes-behavior/144705/1 "2008-08-01T11:03:43Z")

</div>

Okay, let me explain my problem. I’ve got a form that looks like this:

- form\_remote\_for item, :url =\> list\_item\_path(item.list, item) do |f|  
%p  
= f.check\_box :completed, :onclick =\> ‘this.form.onsubmit()’, :index  
=\> item.id  
= f.text\_field :content, :readonly =\> ‘readonly’, :index =\> item.id,  
:onblur =\> ‘this.form.onsubmit()’  
= link\_to\_remote “(edit)”, :url =\> edit\_list\_item\_path(item.list,  
item), :method =\> :get

It’s written in HAML. As you can see there are a couple of special  
things about this form:

1. I’m using the :index option for the fields because there are multiple  
Item forms on the same page.

2. The ‘content’ text field is readonly, but when the user clicks the  
“(edit)” link that attribute will be removed to enable the user to edit  
the field.

3. The form gets submitted when the user clicks the check box or presses  
enter or sets the ‘content’ field _out_ of focus.

The action that the form gets submitted to looks like this:

def update  
@list = current\_user.lists.find(params[:list\_id])  
@item = @list.items.find(params[:id])  
@item.update\_attributes(params[:item][@item.id])  
end

And here is the log message:

Processing ItemsController#update (for 127.0.0.1 at 2008-08-01 10:59:05)  
[PUT]  
Session ID:  
BAh7CDoMY3NyZl9pZCIlNmFiNjYzYmNjNDMwMGY5NzZmZGVkZjcwZTIxZDVk  
YWU6DHVzZXJfaWRpBiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxh  
c2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA==–8c3643dc2753649acded627430ae5f60037b36f8  
Parameters:  
{“authenticity\_token”=\>“d1ada56a0fe0d5d80601c65215ef262e940e56bb”,  
“\_method”=\>“put”, “action”=\>“update”, “id”=\>“37”, “list\_id”=\>“6”,  
“controller”=\>“items”, “item”=\>{“37”=\>{“completed”=\>“0”,  
“content”=\>“abc”}}}  
e[4;36;1mUser Columns (0.015000)e[0m e[0;1mSHOW FIELDS FROM  
`users`e[0m  
e[4;35;1mUser Load (0.000000)e[0m e[0mSELECT \* FROM `users` WHERE  
(`users`.`id` = 1) LIMIT 1e[0m  
e[4;36;1mList Columns (0.000000)e[0m e[0;1mSHOW FIELDS FROM  
`lists`e[0m  
e[4;35;1mList Load (0.000000)e[0m e[0mSELECT \* FROM `lists` WHERE  
(`lists`.`id` = 6 AND (`lists`.user\_id = 1)) e[0m  
e[4;36;1mItem Columns (0.016000)e[0m e[0;1mSHOW FIELDS FROM  
`items`e[0m  
e[4;35;1mItem Load (0.016000)e[0m e[0mSELECT \* FROM `items` WHERE  
(`items`.`id` = 37 AND (`items`.list\_id = 6)) e[0m  
e[4;36;1mSQL (0.000000)e[0m e[0;1mBEGINe[0m  
e[4;35;1mSQL (0.000000)e[0m e[0mCOMMITe[0m  
Rendering items/update  
e[4;36;1mItem Load (0.000000)e[0m e[0;1mSELECT \* FROM `items` WHERE  
(`items`.list\_id = 6) AND (`items`.`completed` = 0) ORDER BY  
positione[0m  
e[4;35;1mList Load (0.000000)e[0m e[0mSELECT \* FROM `lists` WHERE  
(`lists`.`id` = 6) e[0m  
Rendered items/\_item (0.03100)  
e[4;36;1mCACHE (0.000000)e[0m e[0;1mSELECT \* FROM `lists` WHERE  
(`lists`.`id` = 6) e[0m  
Rendered items/\_item (0.00000)  
e[4;35;1mCACHE (0.000000)e[0m e[0mSELECT \* FROM `lists` WHERE  
(`lists`.`id` = 6) e[0m  
Rendered items/\_item (0.01500)  
e[4;36;1mCACHE (0.000000)e[0m e[0;1mSELECT \* FROM `lists` WHERE  
(`lists`.`id` = 6) e[0m  
Rendered items/\_item (0.01600)  
e[4;35;1mCACHE (0.000000)e[0m e[0mSELECT \* FROM `lists` WHERE  
(`lists`.`id` = 6) e[0m  
Rendered items/\_item (0.00000)  
Completed in 0.29700 (3 reqs/sec) | Rendering: 0.11000 (37%) | DB:  
0.04700 (15%) | 200 OK [[http://localhost/lists/6/items/37](http://localhost/lists/6/items/37)]

As you can see the Item is not getting updated at all. But it renders  
the update.rjs template anyway and rerenders all the \_item partials  
which contains the forms I just showed you.

Anyone who can see the problem. Please take a close look at the log  
message like I did. It must have made me blind to stare at this. If you  
need any further information, please just ask.

Thanks in advance,  
David T…

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [August 1, 2008, 11:14am UTC](https://www.ruby-forum.com/t/strange-update-attributes-behavior/144705/2 "2008-08-01T11:14:44Z")

</div>

> As you can see the Item is not getting updated at all. But it renders  
> the update.rjs template anyway and rerenders all the \_item partials  
> which contains the forms I just showed you.

Failing validations ?

Fred

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [August 1, 2008, 11:57am UTC](https://www.ruby-forum.com/t/strange-update-attributes-behavior/144705/3 "2008-08-01T11:57:13Z")

</div>

Frederick C. wrote:

> > As you can see the Item is not getting updated at all. But it renders  
> > the update.rjs template anyway and rerenders all the \_item partials  
> > which contains the forms I just showed you.
> 
> Failing validations ?

No, actually there are no validations to fail. ):

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [August 1, 2008, 12:03pm UTC](https://www.ruby-forum.com/t/strange-update-attributes-behavior/144705/4 "2008-08-01T12:03:23Z")

</div>

On 1 Aug 2008, at 10:57, David T. wrote:

> 

I’d change update\_attributes to update\_attributes! (which will raise  
errors if necessary).  
Also check that activerecord’s (new in 2.1) dirty change tracking  
isn’t tripping you up (save will be a no-op if there aren’t any  
changes to make)

Fred

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [August 1, 2008, 12:14pm UTC](https://www.ruby-forum.com/t/strange-update-attributes-behavior/144705/5 "2008-08-01T12:14:44Z")

</div>

Frederick C. wrote:

> > > > As you can see the Item is not getting updated at all. But it  
> > > > renders  
> > > > the update.rjs template anyway and rerenders all the \_item partials  
> > > > which contains the forms I just showed you.
> > > 
> > > Failing validations ?
> > 
> > No, actually there are no validations to fail. ):
> 
> Another thought, if you have a association called transaction, then  
> scrap it (that breaks stuff because it will replace a method on  
> ActiveRecord)

I don’t. I think I already tried the bang method, but I’ll try again and  
tell you about the result. If anyone’s got an idea, please tell.

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [August 1, 2008, 12:37pm UTC](https://www.ruby-forum.com/t/strange-update-attributes-behavior/144705/6 "2008-08-01T12:37:32Z")

</div>

David T. wrote:

> > Another thought, if you have a association called transaction, then  
> > scrap it (that breaks stuff because it will replace a method on  
> > ActiveRecord)
> 
> I don’t. I think I already tried the bang method, but I’ll try again and  
> tell you about the result. If anyone’s got an idea, please tell.

No, the bang method doesn’t work either… By the way here is the model:

class Item \< ActiveRecord::Base  
belongs\_to :list  
named\_scope :complete, :conditions =\> { :completed =\> 1 }, :order =\>  
:position  
named\_scope :incomplete, :conditions =\> { :completed =\> 0 }, :order =\>  
:position  
end

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [August 1, 2008, 12:04pm UTC](https://www.ruby-forum.com/t/strange-update-attributes-behavior/144705/7 "2008-08-01T12:04:34Z")

</div>

On 1 Aug 2008, at 10:57, David T. wrote:

> Frederick C. wrote:
> 
> > > As you can see the Item is not getting updated at all. But it  
> > > renders  
> > > the update.rjs template anyway and rerenders all the \_item partials  
> > > which contains the forms I just showed you.
> > 
> > Failing validations ?
> 
> No, actually there are no validations to fail. ):

Another thought, if you have a association called transaction, then  
scrap it (that breaks stuff because it will replace a method on  
ActiveRecord)

Fred

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [August 1, 2008, 1:16pm UTC](https://www.ruby-forum.com/t/strange-update-attributes-behavior/144705/8 "2008-08-01T13:16:35Z")

</div>

On 1 Aug 2008, at 11:37, David T. wrote:

> No, the bang method doesn’t work either… By the way here is the  
> model:

In that case I’d try stepping through update\_attributes with the  
debugger and see where it goes wrong.

Fred
