【发布时间】:2016-04-27 14:36:49
【问题描述】:
给出两个模型,具有has_one 关联:
class ShopInfo
belongs_to :shop
end
class Shop
has_one :shop_info
end
s = Shop.create
ss1 = s.create_shop_info
在我的代码的其他地方
ss2 = s.create_shop_info
此后,ss1.shop_id 设置为 nil,因此 ss1 现在是孤儿记录。
有什么办法可以删除以前的记录而不是设置为零?
【问题讨论】:
-
为什么不更新之前的shop_info记录,而不是新建一条呢?
-
我试图阻止其他开发人员在执行此调用时创建孤立记录。对于隔离解决方案,我可以这样做,但我不会在以后的代码中阻止其他开发人员的这种行为。
标签: ruby-on-rails activerecord has-one