gpt4 book ai didi

ruby-on-rails - has_many_polymorphs 是什么意思 "Referential integrity violation"?

转载 作者:太空宇宙 更新时间:2023-11-03 16:12:08 25 4
gpt4 key购买 nike

我在 Candidate 和许多不同类型的事件之间有一个 has_many_polymorphs 关系。特别是,Candidate 在创建时会创建一个 Created 事件。

class Candidate < ActiveRecord::Base
has_many_polymorphs :events, :through => :candidate_events,
:from => Event::Base.included_in_classes.map { |klass|
klass.to_s.underscore.pluralize.to_sym
})
after_validation_on_create :create_created_event

private
def create_creation_event
Event::Created.create!(:candidate => self, :creator => creator)
end
end

class CandidateEvent < ActiveRecord::Base
belongs_to :candidate
belongs_to :event, :polymorphic => true
end

module Event::Base
...
end

class Event::Created < ActiveRecord::Base
include Event::Base
validates_presence_of :creator
end

当我运行单元测试时,一切正常。当我运行我的功能测试时,一切都很好。当我运行我的集成 (Cucumber) 测试时,一切都很好。当我在生产中运行时,一切都很好。当我尝试在开发模式下运行时(打开类重新加载),我得到

Referential integrity violation; child <Event::Created:1> was not found for :events.
Expected record['candidate_events.event_id'] (1) to be equal to record['created_events.id'] ().
{
"candidate_events.event_type"=>"Event::Created",
"candidate_events.created_at"=>"2009-08-05 20:28:31",
"candidate_events.updated_at"=>"2009-08-05 20:28:31",
"candidate_events.candidate_id"=>"1",
"candidate_events.event_id"=>"1",
"candidate_events.id"=>"1"
}

在同一(开发)环境中运行 script/console 我看到 Event::Created 对象与 CandidateEvent 有适当的关系交叉引用模型。

这是怎么回事?

最佳答案

我们能否确定 Event::Base.included_in_classes 在类重新加载时返回正确的类?这种技巧不是依赖于加载顺序吗?即可能 Event::Created 还没有包含 Event::Base?

关于ruby-on-rails - has_many_polymorphs 是什么意思 "Referential integrity violation"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1235607/

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