예약 투어가있는 앱을 만들고 있습니다. 각 투어는 다른 날에 가능하며 각 예약은 하루에 속합니다. 내 질문은 내가 사용하려는 각 모델 (투어 및 예약)에 "일"열을 만들어야합니까, 아니면 아래와 같이 일종의 연결 및 / 또는 조인 테이블을 만들어야합니까?
어느 것이 더 적절한 지 잘 모르겠습니다. 내 목표는 각 투어에 대한 날짜를 추가 한 다음 예약 페이지에 해당 날짜를 표시하고 사용자가 날짜를 선택할 수 있도록하는 것입니다.
모델
예약
투어
days
days_tours (join table)
협회
투어 has_many : days_tours
투어 has_many : days, through : : days_tours
day has_many : days_tours
day has_many : tours, through : : days_tours
예약 has_one : day
user_id, tour_id 및 day_id의 세 가지 외래 키가있는 Reservations 테이블을 사용합니다.
그러면 해당 테이블이 조인 테이블 역할을 할 수 있습니다.
그런 다음 필요에 따라 다음을 설정할 수 있습니다.
# User
has_many :reservations
has_many :tours, through: :reservations
has_many :days, through: :reservations
# Tour
has_many :reservations
has_many :days, through: :reservations
has_many :users, through: :reservations
# Day
has_many :reservations
has_many :users, through: :reservations
has_many :tours, through: :reservations
또한 한쪽에있는 모든 has_many 또는 has_one은 다른쪽에 belongs_to가 필요함을 기억하십시오.
# Reservation
belongs_to :user
belongs_to :tour
belongs_to :day
has_and_belongs_to_many의 경우 설정이 더 간단하지만 특히 두 개 이상의 모델을 결합하는 경우 has_many : through보다 훨씬 덜 유연합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다