1

다형성 연관과 함께 단일 테이블 상속을 사용하고 있습니다. 여기 내 모델입니다.왜 내 polymorphic_type 필드가 레일에 의해 자동 할당되지 않습니까?

class ChangeInformation < ActiveRecord::Base 
    belongs_to :eventable, :polymorphic => true 
end 

class Race < ActiveRecord::Base 
    has_many :track_condition_changes, :as => :eventable, :class_name => "ChangeInformation" 
    #other associations omitted 
end 

class TrackConditionChange < ChangeInformation 

end 

change_informations 테이블에는 다음 필드가 있습니다

type    #sti field 
change_code 
eventalbe_id  #polymorphic id 
eventable_type  #polymorphic type 
description 

내가 만드는 다음과 같은 방법을 사용하면하십시오 TrackConditionChange 기록이 채워 유형 필드가 작성됩니다

TrackConditionChange.create(:change_code => 1, :eventable_id => 3 :description => "test") 

을, 그러나를 eventable_type 필드 (Race 여야 함)는 채워지지 않습니다. 레일이 STI 유형 필드와 자동으로 유사하게이 필드를 채웠다는 느낌을 받았습니다. 내가 잘못된 인상을 받았거나 협동 조합 설치에 문제가 있었습니까?

감사합니다.

답변

4

eventable_id 만 전달하는 경우 어떤 유형인지 어떻게 알 수 있습니까? 당신은에있는 중 전체 eventable 개체를 전달하거나 track_condition_changes 관계에 기반을 구축 할 것입니다 :

1. eventable 개체를 전달합니다

race = Race.find(3) 
TrackConditionChange.create(:change_code => 1, :eventable => race, :description => "test") 

2. 빌드와의 관계에 따라 저장 :

race = Race.find(3) 
race.track_condition_changes << TrackConditionChange.new(:change_code => 1, :description => "test") 
+0

Beerlington - 도와 줘서 고마워. 어떻게 든 레일스가 협회의 유형을 알아낼 수 있다고 확신했지만 더 이상의 반향을 통해 나는 그 문제가 무엇인지 알게되었습니다. 다시 한 번 감사드립니다! – Mutuelinvestor