# Problem with touch and autosave options

**URL:** <https://www.ruby-forum.com/t/problem-with-touch-and-autosave-options/172628>\
**Category:** Rails\
**Created:** [July 24, 2009, 9:04am UTC](https://www.ruby-forum.com/t/problem-with-touch-and-autosave-options/172628 "2009-07-24T09:04:24Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Florent2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/eada6e/32.png) [@Florent2](https://www.ruby-forum.com/u/Florent2)\
**Post date:** [July 24, 2009, 9:04am UTC](https://www.ruby-forum.com/t/problem-with-touch-and-autosave-options/172628/1 "2009-07-24T09:04:24Z")

</div>

Hello,

I face a problem using the new Rails 2.3.3 :touch option along with  
the :autosave option.

My app have three models: User, Group and Membership

class User \< ActiveRecord::Base

# attribute: name

has\_many :memberships, :dependent =\> :destroy  
accepts\_nested\_attributes\_for :memberships, :allow\_destroy =\>  
true, :reject\_if =\> proc { |attributes| attributes  
[‘group\_id’].blank? }

# Having “accepts\_nested\_attributes\_for :memberships”

makes :autosave option true for the “has\_many :memberships”  
association.  
has\_many :groups, :through =\> :memberships  
end

class Membership \< ActiveRecord::Base

# attributes: user\_id, group\_id, content

belongs\_to :user, :touch =\> true  
belongs\_to :group, :touch =\> true  
end

class Group \< ActiveRecord::Base

# attribute: name

has\_many :memberships, :dependent =\> :destroy  
has\_many :users, :through =\> :memberships  
end

You can see my problem in this console script:

> > user = User.find 1  
> > User Load (1.6ms) SELECT \* FROM “users” WHERE (“users”.“id” = 1)  
> > ±—±-----±------------------------±------------------------+  
> > | id | name | created\_at | updated\_at |  
> > ±—±-----±------------------------±------------------------+  
> > | 1 | joe | 2009-07-09 20:47:52 UTC | 2009-07-23 21:59:33 UTC |  
> > ±—±-----±------------------------±------------------------+  
> > 1 row in set
> 
> > user.memberships  
> > Membership Load (1.2ms) SELECT \* FROM “memberships” WHERE  
> > (“memberships”.user\_id = 1)  
> > ±—±---------±---------±--------±------------------------  
> > ±------------------------+  
> > | id | content | group\_id | user\_id | created\_at |  
> > updated\_at |  
> > ±—±---------±---------±--------±------------------------  
> > ±------------------------+  
> > | 1 | the boss | 1 | 1 | 2009-07-21 22:59:21 UTC |  
> > 2009-07-23 21:59:33 UTC |  
> > ±—±---------±---------±--------±------------------------  
> > ±------------------------+  
> > 1 row in set
> 
> > user.name = “bill”  
> > =\> “bill”
> 
> > user.save!  
> > User Update (0.7ms) UPDATE “users” SET “updated\_at” = ‘2009-07-23  
> > 21:59:56’, “name” = ‘bill’ WHERE “id” = 1  
> > User Load (1.0ms) SELECT \* FROM “users” WHERE (“users”.“id” = 1)  
> > User Update (0.3ms) UPDATE “users” SET “updated\_at” = ‘2009-07-23  
> > 21:59:56’ WHERE “id” = 1  
> > Group Load (0.7ms) SELECT \* FROM “groups” WHERE (“groups”.“id” =

1. 

Group Update (0.2ms) UPDATE “groups” SET “updated\_at” =  
‘2009-07-23 21:59:56’ WHERE “id” = 1  
=\> true

As you can see, even if I did not change the user membership, when I  
save the user, it will touch the group of the membership.  
Can I prevent this unexpected behaviour, or is it a bug of the new  
touch feature with autosave?

It does not happen without accepts\_nested\_attributes\_for (ie  
without :autosave) or if I don’t load user.memberships before saving  
user, but in my app I need accepts\_nested\_attributes\_for and the user  
memberships are loaded in the User controller edit method.

Thanks by advance for your help 🙂

Regards,  
Florent
