#168 ✓resolved
Elliott Mason

Unsaved associated records not being saved (belongs_to)

Reported by Elliott Mason | February 24th, 2008 @ 10:11 PM

Another problem related to belongs_to

class Foo < DataMapper::Base
  has_many  :bars

class Bar < DataMapper::Base
  belongs_to :foo

bar = Bar.new
bar.foo = Foo.new

Should save both bar and the associated Foo object but does not, forcing one to bar.foo.save separately to fully complete the association.

def save_without_validation(database_context, cleared)
  @new_member = false
  unless @associated.nil?
    @instance.database_context.adapter.save_without_validation(database_context, @associated, cleared)
    #previously: @instance.database_context.adapter.save_without_validation(database_context, @instance, cleared)

And the spec I added to belongs_to_association_spec.rb

it 'and its unsaved belongs_to associated object should be saved together' do
  database do |db|
    e = Exhibit.new({:name => 'Super Deluxe Gorilla Cage'})
    e.zoo = Zoo.new({:name => 'Gorilla Zoo'})
    e.zoo.new_record?.should == false
    e.zoo.id.should_not == nil
    e.zoo_id.should == e.zoo.id

Like my last ticket I don't know how to make patches in Windows so don't yell at me.

Comments and changes to this ticket

  • Elliott Mason

    Elliott Mason March 13th, 2008 @ 12:31 PM

    Nix that, I didn't fix it. The test I wrote should have reloaded objects before checking if e.zoo_id was set properly. It is set on the object after e.save but not in the actual database, so retrieving that same record zoo_id would be nil. I would imagine it would be because the Zoo is saved after the Exhibit, or something along those lines, which means if I save the Exhibit twice, then zoo_id does actually get set properly. I'll try to fix it for real this time.

  • Dan Kubb (dkubb)
  • Sam Smoot

    Sam Smoot May 21st, 2008 @ 01:16 AM

    • State changed from “new” to “resolved”

    There's been several patches applied to 0.3.0 recently that might have resolved this.

    Either way, should not be an issue in 0.9.0.

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