#236 ✓resolved
Sergei Yakovlev

[Suggestion] Association assignment

Reported by Sergei Yakovlev | May 3rd, 2008 @ 03:55 PM

Allow direct assignment to associations:

company.products = [ Product.new(:title => 'iPhone') ]

This would allow creation of objects and their associated objects in one fell swoop:

Company.create(:title => 'Apple', :products => [ Project.new(:title => 'iPod'), Project.new(:title => 'iPhone') ])

Comments and changes to this ticket

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) May 5th, 2008 @ 05:48 AM

    Sergei, this is something we'd accept a patch for. Would you be able to fork dm-core on github, and work on this?

    Here's a brief overview of how I would implement this:

    You'll want to work in DM::Associations::OneToMany so that an "#{association_name}=" method is created. You'll also want to update DM::Associations::OneToMany::Proxy so that it has a replace() method that calls self.clear() to detach existing resources from the association, then uses append_resource() to "track" the resources in the array (so if the parent is saved, these new children will be saved too). The replace() method should also call @children.replace() with the array of resources; which will cause the underlying Collection object to be updated to reflect the Array that was passed in.

    If this doesn't make any sense please let me know. It should once you start reading the one_to_many.rb file in dm-core.

  • Sam Smoot

    Sam Smoot May 9th, 2008 @ 12:16 AM

    • Milestone cleared.
  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) May 9th, 2008 @ 04:09 AM

    • State changed from “new” to “resolved”

    This ticket was resolved with the following commit:

    http://github.com/sam/dm-core/co...

    Note the last line of the commit description:

    • Allow mass-assign to OneToMany associations

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

Tags

Pages