#810 ✓resolved
Andre Ben Hamou

Associations Insufficiently Lazy

Reported by Andre Ben Hamou | March 2nd, 2009 @ 06:54 PM

Picture, if you will, the following DM models...

class Basket
has n, :apples end

class Apple
belongs_to :basket end

b = Basket.new

Now I want to add some apples...

b.apples.build # or...
b.apples << Apple.new

In either case, the association object inside the Basket starts with @children = nil. As soon as I do either of the Apple-adding operations, the association's 'children' method is invoked (which attempts to grab existing children in order to initialize @children).

The problem here is that both the Basket and the Apple are new_records. I cannot envisage a scenario where it is appropriate to poll the database (as DM currently ends up doing) before the parent object even has an auto ID.

The net result here is that a batch importer I've been writing is firing off millions of unneeded queries when operating.

Comments and changes to this ticket

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) May 28th, 2009 @ 07:14 PM

    • State changed from “unconfirmed” to “accepted”
    • Assigned user set to “Dan Kubb (dkubb)”
  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) June 7th, 2009 @ 02:31 AM

    • State changed from “accepted” to “resolved”

    I can confirm that this is resolved in the dm-core/next branch. It now performs several checks and will not lazy load the collection if the "source" is not yet saved.. since that would be pointless.

    Please test out your code with the dm-core/next branch and add any comments if the problem isn't resolved for you.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket