2014-12-10 2 views
0

다른 폴리 어소시 (poly assoc)에서 가져온 다형성 연관을 정의하는 방법이 있습니까? 그렇다면 첫 번째 폴리 어가 비어 있으면 백업 폴리 어소로 되돌아갑니다. 아마 이것처럼?레일 : 중복 다형성 연관에 폴백을 사용할 수 있습니까?

class Reservation < ActiveRecord::Base 
    has_many :trip_itinerary_entries, :through => :trips, :source => :itinerary_entries 
    has_many :template_itinerary_entries, :through => :templates, :source => :itinerary_entries 
    has_many :entries, :from => :trip_itinerary_entries, :backup => :template_itinerary_entries 

고마워요!

답변

0

두 개의 다형성 연관을 내 모델 및 내 DB에 필수 항목으로 설정 한 다음 두 항목을 모두 가져 오는 메소드를 작성합니다. 그렇지 않으면 메소드를 대체하지 않으려 고합니다. 새로운 관계를 쓸 수 있어야 함).

def get_entries 
    if entries.any? 
    entries 
    else 
    template_itinerary_entries 
    end 
end 

당신이 항목을 추가해야 할 때 당신은 여전히 ​​entries를 사용할 수있는이 방법.

reserve = Reservation.new 
reserve.entries << Entry.create() 

그러나 get_entries 방법을 사용하면 항목 중 하나를 가져올 수 있습니다.

reserve.get_entries 
=> [template_itinerary_entries] # if there are no entries. 
관련 문제