has_many_polymorphs 所说的“引用完整性违规”是什么意思?

发布于 2024-07-30 01:38:00 字数 1688 浏览 7 评论 0原文

我在 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::CreatedCandidateEvent 交叉引用模型具有正确关系的对象。

这是怎么回事?

I have a has_many_polymorphs relationship between a Candidate and many events of various type. In particular, a Candidate creates a Created event when it is 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

When I run my unit tests, everything is fine. When I run my functional tests, everything is fine. When I run my integration (Cucumber) tests, everything is fine. When I run in production, everything is fine. When I try to run in development mode (with class-reloading on), I get

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"
  }

Running script/console in the same (development) environment I see that Event::Created object with the proper relationship to the CandidateEvent cross-reference model.

What's going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云柯 2024-08-06 01:38:00

我们能否确定 Event::Base.included_in_classes 在类重新加载时返回正确的类? 这个技巧不是依赖于加载顺序吗? 即也许 Event::Created 尚未包含 Event::Base?

Can we be sure that Event::Base.included_in_classes is returning the right classes when classes reload? Isn't this trick sort of load order dependent? ie perhaps Event::Created hasn't yet included Event::Base?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文