2011-04-13 3 views
1

나는 다형성 관계가 있으며, 그 아이 (polymorph?)를 완전히 투명하게하고 싶습니다. 셋업은 일반입니다 : 그들은 #starts_at#ends_at 테이블 열을 가진 것처럼 내가 좋아하는 것Rails transparent child relationship

class ScheduledEvent < ActiveRecord::Base 
    belongs_to :scheduleable, polymorphic:true 
    #has column names like #starts_at, #ends_at 
end 
class AppointmentTypeOne < ActiveRecord::Base 
    has_one :scheduled_event, :as=>:scheduleable, :dependent=>:destroy 
end 
class AppointmentTypeTwo < ActiveRecord::Base 
    has_one :scheduled_event, :as=>:scheduleable, :dependent=>:destroy 
end 

AppointmentTypeOneAppointmentTypeTwo을 처리 할 수 ​​있도록.

방법이 많다는 내 AppointmentX 클래스에 #starts_at, #starts_at= 등을 추가하고 ScheduledEvent 다시 참조하지만 매우 간단합니다. 그러나 관계가 ActiveRelation에도 투명하도록 어떻게 설정할 수 있습니까? 당신은 단일 테이블 상속이 아닌 has_one 연결을 사용하려는 것처럼 소리

답변

0

(join 또는 include:scheduled_event에 있지 않는)

AppointmentTypeOne.where('starts_at IS NOT NULL') 

: 내가 그런 짓을시키는.

class ScheduledEvent < ActiveRecord::Base 

end 

class AppointmentTypeOne < ScheduledEvent 

end 

class AppointmentTypeTwo < ScheduledEvent 

end 

기본적으로, 당신은 당신의 scheduled_events 테이블에 유형 열을 추가하고, 레일은 나머지 처리한다 : 즉, 각 예약 유형에 대한 ScheduledEvent의 하위 클래스를 만들 수 있습니다.

이 포럼 게시물은 모든 세부 정보를 다루고 있습니다 : http://railsforum.com/viewtopic.php?id=3815