Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #9989 (closed defect: fixed)

Opened 2 years ago

Last modified 2 years ago

[PATCH] ActiveRecord Association << Operator not working with unsaved Objects in EdgeRails (and 2.0PR)

Reported by: lennartmelzer Assigned to: core
Priority: normal Milestone: 2.0
Component: ActiveRecord Version: edge
Severity: normal Keywords:
Cc: michael@novemberain.com, jarkko, evan

Description

Hi there,

I'm having problems with the << operator in ActiveRecord objects in EdgeRails 2.0 Preview Release. I checked the same code in 1.2.3 and it works there.

Given a simple Document has_many Elements/ Element belongs_to Document Association the following doesn't work, though throwing no error:

p = Document.new

p.elements << Element.new

p.elements

$ []

In the current Stable the Element gets stored in the unsaved Object.

Any suggestions why the above doesn't work, maybe I'm missing a new way of how Rails 2.0 is supposed to work

regards

Lennart

Attachments

load_assoc_for_new_record_to_avoid_loss.diff (1.3 kB) - added by hasmanyjosh on 10/28/07 01:46:13.

Change History

10/25/07 23:21:05 changed by evolving_jerk

  • cc set to michael@novemberain.com.

10/26/07 13:36:24 changed by danger

Just a note: this bug is also identified in #9987

10/26/07 13:37:21 changed by danger

Thanks for reporting this Lennart - and, no, I don't think Rails 2.0 shouldn't be working like this.

10/26/07 21:08:44 changed by jarkko

  • cc changed from michael@novemberain.com to michael@novemberain.com, jarkko.

10/26/07 22:01:57 changed by hasmanyjosh

This bug only manifests if the has_many association is unloaded. So this works:

p = Document.new
p.elements
p.elements << Element.new
p.elements
#=> [#<Element...>]

10/27/07 05:40:12 changed by hasmanyjosh

This looks like it's related to [5769], which was backed out of the stable branch. The missing load_target line is probably the culprit.

10/27/07 10:42:02 changed by evan

  • cc changed from michael@novemberain.com, jarkko to michael@novemberain.com, jarkko, evan.

10/28/07 01:46:13 changed by hasmanyjosh

  • attachment load_assoc_for_new_record_to_avoid_loss.diff added.

10/28/07 01:49:08 changed by hasmanyjosh

  • summary changed from ActiveRecord Association << Operator not working with unsaved Objects in EdgeRails (and 2.0PR) to [PATCH] ActiveRecord Association << Operator not working with unsaved Objects in EdgeRails (and 2.0PR).

Here's a patch that modifies the culprit. The problem was that objects were being added to the association, then later the association would be loaded and overwrite those contents. Now it will "load" the empty association for new records, preventing the overwriting later. However, this should still provide the optimization of not loading unnecessarily for existing records which may have many association contents.

10/28/07 03:16:07 changed by bitsweat

This means loading all of josh.posts just to add a new record; this is the problem [5769] hoped to solve.

10/28/07 03:37:48 changed by nzkoz

but if @owner.new_record? is true, then there literally can't be any records in that association. It doesn't have an id.

Looks good to me

10/28/07 03:39:21 changed by nzkoz

  • status changed from new to closed.
  • resolution set to fixed.

(In [8049]) Make sure that << works on has_many associations on unsaved records. Closes #9989 [hasmanyjosh]