Model.update and associations

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.

On Mar 26, 7:44 pm, Simon R. [email protected]
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

Frederick C. wrote:

On Mar 26, 7:44�pm, Simon R. [email protected]
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

Frederick C. wrote:

On Mar 26, 7:44�pm, Simon R. [email protected]
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
localese[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
userse[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
sportse[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_sportse[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
groupse[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_sportse[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_sportse[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_sportse[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_sportse[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.

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