我有一個和許多不同類型的事件has_many_polymorphs
之間的關係。特別是,Candidate
創建時會創建Created
事件。has_many_polymorphs是什麼意思「參照完整性違反」?
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
當我運行我的單元測試時,一切都很好。當我運行我的功能測試時,一切都很好。當我運行我的集成(黃瓜)測試時,一切都很好。當我在生產中運行時,一切都很好。當我嘗試在開發模式下運行(與類重裝上),我得到
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
交叉參考模型的正確關係。
發生了什麼事?
你能向我們展示一些sql查詢的結果嗎?在你的開發數據庫的這些表中的相關行? – 2009-08-05 21:47:49
他們很簡單。我只有一行,每個都有適當的外鍵(全部爲「1」) – 2009-08-06 11:51:06