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
答案 0 :(得分:0)
从已编辑的问题正文中复制答案,以便从“未答复”过滤器中删除此问题:
回答答案是在销毁Beta后重新加载Alpha对象
puts @alpha.betas.inspect puts "Destroyed? #{@alpha.betas.first.destroy}" puts @alpha.betas.reload puts @alpha.betas.inspect