2017-12-25 1 views
1

레일 문제보다 "모델 디자인"문제에 더 가깝습니다.레일은 서브 STI가있는 STI has_many

여기에 명확한 비즈니스 로직이 있습니다. Venue가 있고 그 장소에 대한 데이터를 얻기 위해 여러 API를 구현하고 싶습니다. 이 모든 API는 공통점이 많으므로 STI를 사용했습니다.

# /app/models/venue.rb 
class Venue < ApplicationRecord 
    has_one :google_api 
    has_one :other_api 
    has_many :apis 
end 

# /app/models/api.rb 
class Api < ApplicationRecord 
    belongs_to :venue 
end 

# /app/models/google_api.rb 
class GoogleApi < Api 
    def find_venue_reference 
    # ... 
    end 
    def synch_data 
    # ... 
    end 
end 

# /app/models/other_api.rb 
class OtherApi < Api 
    def find_venue_reference 
    # ... 
    end 
    def synch_data 
    # ... 
    end 
end 

그 부분이 작동합니다. 이제는 내가 추가하려고하는 부분이 현장 사진입니다. API에서 가져온 사진을 가져와서 모든 API가 다를 수 있음을 알고 있습니다. 나뿐만 아니라 그것을 위해 STI를 사용하여 생각 나는 ApplicationRecord 말 #의 /app/models/venue.rb 클래스 장소 <에서이 가지고있는 그

# /app/models/api_photo.rb 
class ApiPhoto < ApplicationRecord 
    belongs_to :api 
end 

# /app/models/google_api_photo.rb 
class GoogleApiPhoto < ApiPhoto 
    def url 
    "www.google.com/#{reference}" 
    end 
end 

# /app/models/other_api_photo.rb 
class OtherApiPhoto < ApiPhoto 
    def url 
    self[url] || nil 
    end 
end 

내 목표 같은 것을 끝낼 것 has_one : google_api has_one : other_api has_many : API를 has_many : 사진 : 통한 => : API를 끝

# /app/views/venues/show.html.erb 
<%# ... %> 
@venue.photos.each do |photo| 
    photo.url 
end 
<%# ... %> 

그리고 photo.url 나에게 바로 formattin을 줄 것이다 g는 api에 종속적입니다.

통합 과정이 깊어 감에 따라 뭔가 잘못된 것으로 보입니다. Api의 경우 has_many :google_api_photo이면 모든 API에 GoogleApiPhoto가 표시됩니다. 나에게 의미가없는 것은 무엇인가.

여기부터 어떻게해야할까요?

답변

0

나는 그것을 풀 었다고 생각한다. ApiPhoto

type 필드에 기초 venue.photos[0].url 호 오른쪽 클래스를 호출하여 venue.rb

has_many :apis, :dependent => :destroy 
has_many :photos, :through => :apis, :source => :api_photos 

이 추가하여

관련 문제