모두에게 새해 복 많이 받으세요!레일 5 : "belongs_to through"제휴 종류
나는 고전 has_many through association 있습니다
class Physician < ApplicationRecord
has_many :appointments
has_many :patients, through: :appointments
end
class Appointment < ApplicationRecord
belongs_to :physician
belongs_to :patient
end
class Patient < ApplicationRecord
has_many :appointments
has_many :physicians, through: :appointments
end
그러나 지금은 다른 모델과 belongs_to
관련되는 일부 class Example
에 class Appointment
에서 has_many
연결을 추가 할 필요 class Example
와 belongs_to
관련 될 것이다.
가능한 경우 이러한 종류의 분류를 설정하는 방법은 무엇입니까? 고맙습니다.
이 질문을 downvoted 왜 업데이트
이해하지 않습니다. 여기 내가 class Example
에 필요한 것입니다 : 2
확인
class Example < ApplicationRecord
belongs_to :appointment
belongs_to :model_bbb
end
업데이트, 나는 this answer에서 솔루션을 사용할 수 있습니다 알아 냈어. 기본적으로 나는 "약속"모델을 삭제하고이 같은 class Example
을 가질 수
class Example < ApplicationRecord
belongs_to :physician
belongs_to :patient
belongs_to :model_bbb
end
그리고 의사 및 환자에 내가 has_many :examples
다른 through
관계를 할 수 있습니다. 나는 class Appointment
의 상대적으로 작은 테이블을 가질 수 있었기 때문에 다소 이상한 belongs_to through
것을하고 싶었지만, class Example
테이블이 꽤 큰 것으로 예상됩니다. 그래서 제 처음 생각은 여러 번 복제 될 여분의 칼럼을 만드는 것이 아니 었습니다.
질문을 이해할 수 없습니다. 마지막 스 니펫에 어떤 문제가 있습니까? 그건 그렇고, 분류를 개선하기 위해 태그를 편집하십시오 - 이것은 ruby-on-rails-5 스레드이기 때문에. – marmeladze