3
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, DataMapper isn't deleting the associated Beta object.

Is this a bug or am I missing something?

A complete example is in this gist 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
matt
  • 74,317
  • 7
  • 140
  • 183
Craig552uk
  • 591
  • 1
  • 4
  • 13
  • If you're game to recap the solution as your own answer, I'll delete my answer. (See http://meta.stackexchange.com/questions/90263/unanswered-question-answered-in-comments for elaboration of why this is helpful.) Thanks! – DreadPirateShawn Oct 10 '13 at 06:32

1 Answers1

0

Copying the answer from the edited question body in order to remove this question from the "Unanswered" 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

Community
  • 1
  • 1
DreadPirateShawn
  • 7,573
  • 4
  • 46
  • 68