DataMapper one-to-many removes crash

class Alpha
  include DataMapper::Resource
  property :id,   Serial
  property :name, String
  has n, :betas
end

class Beta
  include DataMapper::Resource
  property :id,   Serial
  property :name, String
  belongs_to :alpha
end

# Create an Alpha with two Betas
@alpha = Alpha.new(:name => 'A')
@alpha.betas << Beta.new(:name => 'B')
@alpha.betas << Beta.new(:name => 'C')
@alpha.save

puts @alpha.betas.inspect
puts "Destroyed? #{@alpha.betas.first.destroy}"
puts @alpha.betas.inspect

      

For some reason, the DataMapper is not deleting the Beta object associated with it.

Is this a bug or am I missing something?

A complete example is given in this document https://gist.github.com/2219479

EDIT

The answer is to reload the Alpha object after destroying the beta

puts @alpha.betas.inspect
puts "Destroyed? #{@alpha.betas.first.destroy}"
puts @alpha.betas.reload
puts @alpha.betas.inspect

      

+3


source to share


1 answer


Copying the answer from the edited question body to remove that question from the No Answer filter:

The answer is to reload the Alpha object after destroying the beta

puts @alpha.betas.inspect
puts "Destroyed? #{@alpha.betas.first.destroy}"
puts @alpha.betas.reload
puts @alpha.betas.inspect

      



~ answer per Craig552uk

0


source







All Articles