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?