# Model.update and associations

**URL:** <https://www.ruby-forum.com/t/model-update-and-associations/133023>\
**Category:** Rails\
**Created:** [March 26, 2008, 8:44pm UTC](https://www.ruby-forum.com/t/model-update-and-associations/133023 "2008-03-26T20:44:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![simonr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@simonr](https://www.ruby-forum.com/u/simonr)\
**Post date:** [March 26, 2008, 8:44pm UTC](https://www.ruby-forum.com/t/model-update-and-associations/133023/1 "2008-03-26T20:44:16Z")

</div>

Hi all,

Just a strange little one I came across this afternoon, I’d much  
appreciate any thoughts any of you might have on it…

I was just implementing a small AJAX list sorter on a project, however  
when I passed the “sortable\_list” array and loop through it the .update  
method seems to ignore associations on the object, and therefore they  
get deleted.

Here’s the scenario:

A Sport (model) has\_and\_belongs\_to\_many Locales and Groups. Standard  
HABTM association. The association data is present and working.

However the following is generated from a Sport.update(2, :position =\>  
3) call in the console:

#\<Sport id: 2, created\_at: “2008-03-14 12:50:23”, updated\_at:  
“2008-03-26 19:33:37”, name: "Rugby, position: 3\>

Is this intentional that the associations are not included, and hence  
removed?

I’ve made a dirty hack to make this work, it’s a bit fugly, but I’m  
confused as to why I was getting the behaviour with .update above? Can  
anyone shed some light?

Thanks,  
Si.

---

<div class="post-metadata">

**Author:** ![simonr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@simonr](https://www.ruby-forum.com/u/simonr)\
**Post date:** [March 26, 2008, 10:18pm UTC](https://www.ruby-forum.com/t/model-update-and-associations/133023/2 "2008-03-26T22:18:42Z")

</div>

On Mar 26, 7:44 pm, Simon R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi all,
> 
> Just a strange little one I came across this afternoon, I’d much  
> appreciate any thoughts any of you might have on it…
> 
> I was just implementing a small AJAX list sorter on a project, however  
> when I passed the “sortable\_list” array and loop through it the .update  
> method seems to ignore associations on the object, and therefore they  
> get deleted.

Doesn’t sound very normal. What’s in your log files when this happens?

Fred

---

<div class="post-metadata">

**Author:** ![simonr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@simonr](https://www.ruby-forum.com/u/simonr)\
**Post date:** [March 27, 2008, 12:33pm UTC](https://www.ruby-forum.com/t/model-update-and-associations/133023/3 "2008-03-27T12:33:11Z")

</div>

Frederick C. wrote:

> On Mar 26, 7:44ï¿½pm, Simon R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Hi all,
> > 
> > Just a strange little one I came across this afternoon, I’d much  
> > appreciate any thoughts any of you might have on it…
> > 
> > I was just implementing a small AJAX list sorter on a project, however  
> > when I passed the “sortable\_list” array and loop through it the .update  
> > method seems to ignore associations on the object, and therefore they  
> > get deleted.
> 
> Doesn’t sound very normal. What’s in your log files when this happens?
> 
> Fred

Sorry horrible paste there, see pastie

[http://pastie.caboo.se/171470](http://pastie.caboo.se/171470)

---

<div class="post-metadata">

**Author:** ![simonr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@simonr](https://www.ruby-forum.com/u/simonr)\
**Post date:** [March 27, 2008, 12:30pm UTC](https://www.ruby-forum.com/t/model-update-and-associations/133023/4 "2008-03-27T12:30:29Z")

</div>

Frederick C. wrote:

> On Mar 26, 7:44ï¿½pm, Simon R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Hi all,
> > 
> > Just a strange little one I came across this afternoon, I’d much  
> > appreciate any thoughts any of you might have on it…
> > 
> > I was just implementing a small AJAX list sorter on a project, however  
> > when I passed the “sortable\_list” array and loop through it the .update  
> > method seems to ignore associations on the object, and therefore they  
> > get deleted.
> 
> Doesn’t sound very normal. What’s in your log files when this happens?
> 
> Fred

That’s what I thought, I’m keen to get to the bottom of it:

Here’s what happens when the action update\_positions is fired from the  
AJAX sortable list (this is using a slightly different dataset)

## Processing SportsController#update\_positions (for 127.0.0.1 at 2008-03-26 19:40:58) [POST] Session ID: BAh7CToJdXNlcmkGOgxjb3VudHJ5IgdpZToOcmV0dXJuX3RvMCIKZmxhc2hJ%0AQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNoSGFzaHsABjoKQHVz%0AZWR7AA%3D%3D–b54e6fd96bf73b96a15f6cfa6ed4fc24dcf1e642 Parameters: {“action”=\>“update\_positions”, “sortable\_list”=\>[“3”, “2”, “4”, “5”, “6”, “7”, “8”, “9”, “10”, “11”], “controller”=\>“admin/sports”} e[4;35;1mLocale Load (0.000762)e[0m e[0mSELECT \* FROM `locales` e[0m e[4;36;1mLocale Columns (0.002516)e[0m e[0;1mSHOW FIELDS FROM `locales`e[0m e[4;35;1mLocale Load (0.001546)e[0m e[0mSELECT \* FROM `locales` WHERE (`locales`.`country_code` = ‘ie’) LIMIT 1e[0m e[4;36;1mGroup Load (0.000514)e[0m e[0;1mSELECT \* FROM `groups` e[0m e[4;35;1mCACHE (0.000000)e[0m e[0mSELECT \* FROM `locales` WHERE (`locales`.`country_code` = ‘ie’) LIMIT 1e[0m e[4;36;1mUser Columns (0.003802)e[0m e[0;1mSHOW FIELDS FROM `users`e[0m e[4;35;1mUser Load (0.001512)e[0m e[0mSELECT \* FROM `users` WHERE (`users`.`id` = 1) LIMIT 1e[0m e[4;36;1mSport Columns (0.003293)e[0m e[0;1mSHOW FIELDS FROM `sports`e[0m e[4;35;1mSport Load (0.000893)e[0m e[0mSELECT \* FROM `sports` WHERE (`sports`.`id` = 3) e[0m e[4;36;1mJoin Table Columns (0.002549)e[0m e[0;1mSHOW FIELDS FROM `groups_sports`e[0m e[4;35;1mGroup Load (0.002243)e[0m e[0mSELECT \* FROM `groups` INNER JOIN groups\_sports ON groups.id = groups\_sports.group\_id WHERE (groups\_sports.sport\_id = 3 ) e[0m e[4;36;1mGroup Columns (0.002650)e[0m e[0;1mSHOW FIELDS FROM `groups`e[0m e[4;35;1mSQL (0.001031)e[0m e[0mBEGINe[0m e[4;36;1mSQL (0.000315)e[0m e[0;1mDELETE FROM groups\_sports WHERE sport\_id = 3 AND group\_id IN (1,2)e[0m e[4;35;1mSQL (0.000740)e[0m e[0mCOMMITe[0m e[4;36;1mJoin Table Columns (0.002419)e[0m e[0;1mSHOW FIELDS FROM `locales_sports`e[0m e[4;35;1mLocale Load (0.001201)e[0m e[0mSELECT \* FROM `locales` INNER JOIN locales\_sports ON locales.id = locales\_sports.locale\_id WHERE (locales\_sports.sport\_id = 3 ) e[0m e[4;36;1mSQL (0.000222)e[0m e[0;1mBEGINe[0m e[4;35;1mSQL (0.000386)e[0m e[0mDELETE FROM locales\_sports WHERE sport\_id = 3 AND locale\_id IN (1,2)e[0m e[4;36;1mSQL (0.000784)e[0m e[0;1mCOMMITe[0m e[4;35;1mSQL (0.000225)e[0m e[0mBEGINe[0m e[4;36;1mSport Update (0.000829)e[0m e[0;1mUPDATE `sports` SET `created_at` = ‘2008-03-15 02:37:00’, `position` = 1, `updated_at` = ‘2008-03-26 19:40:58’, `name` = ‘Rugby’ WHERE `id` = 3e[0m e[4;35;1mSQL (0.000668)e[0m e[0mCOMMITe[0m e[4;36;1mSport Load (0.000668)e[0m e[0;1mSELECT \* FROM `sports` WHERE (`sports`.`id` = 3) e[0m e[4;35;1mSQL (0.000156)e[0m e[0mBEGINe[0m e[4;36;1mSport Update (0.000392)e[0m e[0;1mUPDATE `sports` SET `created_at` = ‘2008-03-15 02:37:00’, `position` = 1, `updated_at` = ‘2008-03-26 19:40:58’, `name` = ‘Rugby’ WHERE `id` = 3e[0m e[4;35;1mSQL (0.000212)e[0m e[0mCOMMITe[0m e[4;36;1mSport Load (0.000587)e[0m e[0;1mSELECT \* FROM `sports` WHERE (`sports`.`id` = 2) e[0m e[4;35;1mJoin Table Columns (0.002631)e[0m e[0mSHOW FIELDS FROM `groups_sports`e[0m e[4;36;1mGroup Load (0.001124)e[0m e[0;1mSELECT \* FROM `groups` INNER JOIN groups\_sports ON groups.id = groups\_sports.group\_id WHERE (groups\_sports.sport\_id = 2 ) e[0m e[4;35;1mSQL (0.000221)e[0m e[0mBEGINe[0m e[4;36;1mSQL (0.000365)e[0m e[0;1mDELETE FROM groups\_sports WHERE sport\_id = 2 AND group\_id IN (1,2)e[0m e[4;35;1mSQL (0.000702)e[0m e[0mCOMMITe[0m e[4;36;1mJoin Table Columns (0.002110)e[0m e[0;1mSHOW FIELDS FROM `locales_sports`e[0m e[4;35;1mLocale Load (0.001991)e[0m e[0mSELECT \* FROM `locales` INNER JOIN locales\_sports ON locales.id = locales\_sports.locale\_id WHERE (locales\_sports.sport\_id = 2 ) e[0m e[4;36;1mSQL (0.000253)e[0m e[0;1mBEGINe[0m e[4;35;1mSQL (0.000351)e[0m e[0mDELETE FROM locales\_sports WHERE sport\_id = 2 AND locale\_id IN (1)e[0m e[4;36;1mSQL (0.000850)e[0m e[0;1mCOMMITe[0m e[4;35;1mSQL (0.000321)e[0m e[0mBEGINe[0m e[4;36;1mSport Update (0.000456)e[0m e[0;1mUPDATE `sports` SET `created_at` = ‘2008-03-14 22:50:23’, `position` = 2, `updated_at` = ‘2008-03-26 19:40:58’, `name` = ‘Off Field’ WHERE `id` = 2e[0m e[4;35;1mSQL (0.000661)e[0m e[0mCOMMITe[0m e[4;36;1mSport Load (0.000821)e[0m e[0;1mSELECT \* FROM `sports` WHERE (`sports`.`id` = 2) e[0m e[4;35;1mSQL (0.000150)e[0m e[0mBEGINe[0m e[4;36;1mSport Update (0.000262)e[0m e[0;1mUPDATE `sports` SET `created_at` = ‘2008-03-14 22:50:23’, `position` = 2, `updated_at` = ‘2008-03-26 19:40:58’, `name` = ‘Off Field’ WHERE `id` = 2e[0m e[4;35;1mSQL (0.000149)e[0m e[0mCOMMITe[0m e[4;36;1mSport Load (0.000529)e[0m e[0;1mSELECT \* FROM `sports` WHERE (`sports`.`id` = 4) e[0m e[4;35;1mJoin Table Columns (0.003138)e[0m e[0mSHOW FIELDS FROM `groups_sports`e[0m e[4;36;1mGroup Load (0.001094)e[0m e[0;1mSELECT \* FROM `groups` INNER JOIN groups\_sports ON groups.id = groups\_sports.group\_id WHERE (groups\_sports.sport\_id = 4 ) e[0m

This continues and loops through every sport (listed by id in the  
sortable\_list array) systematically changing the position correctly but  
also deleting the group and locale associations…

My mind isn’t any clearer this morning on it?

Si.
