Ruby on rails Rails 4.2 dependent::销毁CollectionProxy的问题
我以前在模型上使用过Ruby on rails Rails 4.2 dependent::销毁CollectionProxy的问题,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我以前在模型上使用过dependent::destroy,没有任何问题,但在rails 4.2中我被卡住了。过去的用途主要是用于经典车型。似乎有时候,当你被困在圈子里时,你只需要问一个问题。当你没有得到答案时,你就有时间重新思考这个问题。我想我没有尝试所有的选择 我认为问题在于我不理解默认的无效策略。根据我的目标,没有财产就没有价值,反之亦然。尝试使用直通关联进行销毁将引发pg错误。我显然没有尝试的简单解决方案是在主题模型中依赖销毁属性,在属性模型中依赖销毁值。指南中关于不要在关联中使用依赖销毁
dependent::destroy
,没有任何问题,但在rails 4.2中我被卡住了。过去的用途主要是用于经典车型。似乎有时候,当你被困在圈子里时,你只需要问一个问题。当你没有得到答案时,你就有时间重新思考这个问题。我想我没有尝试所有的选择
我认为问题在于我不理解默认的无效策略。根据我的目标,没有财产就没有价值,反之亦然。尝试使用直通关联进行销毁将引发pg错误。我显然没有尝试的简单解决方案是在主题模型中依赖销毁属性,在属性模型中依赖销毁值。指南中关于不要在关联中使用依赖销毁的警告可能已开始我的循环行程。我仍然不确定我是否理解这个警告。我的猜测是,当subject.properties
被销毁时,subject\u id在调用property.value
destroy之前被设置为null,从而避免了pg错误。我的清理模型:
class Subject < ActiveRecord::Base
has_many :properties, dependent: :destroy
has_many :values, :through => :properties
has_many :tags, :through => :properties
class Property < ActiveRecord::Base
belongs_to :subject
belongs_to :tag
belongs_to :value, dependent: :destroy
class Value < ActiveRecord::Base
has_one :property
has_one :subject, :through => :property
has_one :tag, :through => :property
类主题:属性
有多个:标记,:到=>:属性
类属性:property
有一个:标记,:通过=>:property
# In Subject model
before_destroy :destroy_values
def destroy_values
# relations does not seem to work got the Values using a new query
#values.destroy_all
pids = values.pluck(:id)
Value.where(id:pids).destroy_all
end
# in Value model
before_destroy :destroy_property
def destroy_property
property.destroy
end
class Subject < ActiveRecord::Base
has_many :properties, dependent: :destroy
has_many :values, :through => :properties
has_many :tags, :through => :properties
class Property < ActiveRecord::Base
belongs_to :subject
belongs_to :tag
belongs_to :value, dependent: :destroy
class Value < ActiveRecord::Base
has_one :property
has_one :subject, :through => :property
has_one :tag, :through => :property