gpt4 book ai didi

ruby - DataMapper 一对多删除失败

转载 作者:数据小太阳 更新时间:2023-10-29 08:13:20 25 4
gpt4 key购买 nike

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

最佳答案

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

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

~ 根据 Craig552uk 回答

关于ruby - DataMapper 一对多删除失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9896325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com