#1154 ✓resolved
Greg Campbell

Excessive validation when creating multiple children at once

Reported by Greg Campbell | December 15th, 2009 @ 03:54 PM | in 1.0.0

The following code causes the parent in a one-to-many relationship to be validated once for every child being created.

require 'rubygems'
require 'dm-core'
require 'dm-validations'

DataMapper::Logger.new(STDOUT, :debug)
DataMapper.setup(:default, 'sqlite3::memory')


class Zoo
  include DataMapper::Resource

  property :id, Serial
  property :name, String, :unique_index => true
  #before :valid?, :validating

  validates_is_unique :name
  
  has n, :animals

end

class Animal
  include DataMapper::Resource
  property :id, Serial
  property :species, String

  belongs_to :zoo
end

DataMapper.auto_migrate!

z = Zoo.new(:name => "name")
50.times {|i| z.animals.new(:species => i.to_s)}
z.save #Note that the uniqueness query on Zoo is performed once per animal: SELECT "id" FROM "zoos" WHERE "name" = 'name' ORDER BY "id" LIMIT 1

Comments and changes to this ticket

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 »

Referenced by

Pages