
Child with unsaved parent is invalid even when the parent is valid
Reported by Ashley Moran | July 7th, 2009 @ 04:38 AM
The examples below demonstrate this bug. What is happening is
that a Child (except when #953 strikes) considers itself invalid when
it has a nil parent_id
. I believe that when a parent
object is associated to the child but new, the valid?
check on the child should either: check that there is a
parent associated, or, check a parent is associated and
also that it is valid.
In fact, I am starting to think that the definition of validity should never rely on the ID, because this should always be obtainable when a valid parent is saved.
Note that this line of thought taken to its logical conclusion means that checking if a child object is valid would involve checking the validity of the entire object graph. (The same issues as found in #940 would apply. This may also extend to the discussion at http://groups.google.com/group/datamapper/t/a80a60b7a3663ef5 about repository-wide operations.)
require 'rubygems'
USE_DM_0_9 = false
if USE_DM_0_9
DM_GEMS_VERSION = "0.9.11"
DO_GEMS_VERSION = "0.9.12"
else
DM_GEMS_VERSION = "0.10.0"
DO_GEMS_VERSION = "0.10.0"
end
gem "data_objects", DO_GEMS_VERSION
gem "do_sqlite3", DO_GEMS_VERSION # If using another database, replace this
gem "dm-core", DM_GEMS_VERSION
gem "dm-types", DM_GEMS_VERSION
gem "dm-validations", DM_GEMS_VERSION
require "data_objects"
require "dm-core"
require "dm-aggregates"
require "dm-migrations"
require "dm-timestamps"
require "dm-types"
require "dm-validations"
require "dm-serializer"
require 'spec'
SQLITE_FILE = File.join(`pwd`.chomp, "test.db")
DataMapper.setup(:default, "sqlite3:#{SQLITE_FILE}")
DataMapper.setup(:reloaded, "sqlite3:#{SQLITE_FILE}")
class Parent
include DataMapper::Resource
property :id, Serial
has n, :children
end
class Child
include DataMapper::Resource
property :id, Serial
belongs_to :parent
has n, :pets
end
module IdentityMapHelper
def reload(object)
object.class.get(object.id)
end
def with_db_reconnect(&blk)
original_repository = DataMapper::Repository.context.pop
repository(:reloaded, &blk)
DataMapper::Repository.context << original_repository
end
end
Spec::Runner.configure do |config|
include IdentityMapHelper
config.before(:each) do
DataMapper.auto_migrate!
end
config.before(:each) do
DataMapper::Repository.context << repository(:default)
end
config.after(:each) do
DataMapper::Repository.context.pop
end
end
describe "Child with unsaved parent" do
before(:each) do
@child = Child.new
@parent = Parent.new
@parent.children << @child
end
it "should define a new child with an assigned but unsaved parent as valid" do
@parent.should be_valid
@child.should be_valid
end
it "works when you save the parent" do
@parent.should be_valid
@parent.save
@parent.should be_valid
@child.should be_valid
end
end
Comments and changes to this ticket
-
Dan Kubb (dkubb) July 16th, 2009 @ 01:10 AM
- Assigned user set to Dan Kubb (dkubb)
- State changed from new to unconfirmed
-
Dan Kubb (dkubb) July 22nd, 2009 @ 04:02 PM
- State changed from unconfirmed to accepted
-
Dan Kubb (dkubb) October 4th, 2009 @ 09:33 PM
- Milestone changed from 0.10.0 to 0.10.2
[project:id#20609 not-tagged:"0.10.0" milestone:id#51895 bulk edit command]
-
Dan Kubb (dkubb) February 1st, 2010 @ 04:33 PM
- Milestone changed from 0.10.2 to 1.0.0
-
-
-
Dan Kubb (dkubb) February 2nd, 2010 @ 02:47 AM
- State changed from accepted to confirmed
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 »