DataMapper一对多删除失败

时间:2012-03-27 19:29:06

标签: ruby associations datamapper ruby-datamapper

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

由于某种原因,DataMapper不会删除关联的Beta对象。

这是一个错误还是我错过了什么?

完整的例子就在这个要点https://gist.github.com/2219479

修改

答案是在销毁Beta后重新加载Alpha对象

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

1 个答案:

答案 0 :(得分:0)

从已编辑的问题正文中复制答案,以便从“未答复”过滤器中删除此问题:

  

答案是在销毁Beta后重新加载Alpha对象

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

〜每Craig552uk

回答