
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
end
class Bar < DataMapper::Base
belongs_to :foo
end
bar = Bar.new
bar.foo = Foo.new
bar.save
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.instance_variable_set(
association.foreign_key_column.instance_variable_name,
@associated.key
)
@instance.database_context.adapter.save_without_validation(database_context, @associated, cleared)
#previously: @instance.database_context.adapter.save_without_validation(database_context, @instance, cleared)
end
end
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.save
e.zoo.new_record?.should == false
e.zoo.id.should_not == nil
e.zoo_id.should == e.zoo.id
end
end
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 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) April 15th, 2008 @ 03:09 AM
- Milestone cleared.
-
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.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »