저는 RoR로 첫 프로젝트를 진행하고 있으며 두 모델 간의 많은 관계를 만들 필요가 있지만 두 번째 모델과 관련된 첫 번째 모델의 개체를 주문할 가능성이 있습니다. 예를 들어두 번째 모델과 연관되어 첫 번째 모델을 주문하십시오.
-Route 1
--Customer 2, position 1
--Customer 1, position 2
-Route 2
--Customer 3, position 1
--Customer 1, position 2
제가 생각하기 때문에, 경로
나는 많은 경로로하지만,이 협회의 순서를 저장하여 많은 고객을 할당합니다 - 고객 -
의 나는 다음과 같은 두 가지 모델 을한다고 가정 해 봅시다 나는 has_many를 사용해야한다 : through와 belongs_to 그리고 creat "position"필드를 in-middle-table에 넣어야하지만이 필드를 접근 가능하고 편집 가능하게 만드는 방법은 무엇인가?
이class Route < ActiveRecord::Base
has_many :bookings
has_many :routes, :through => bookings
end
class Booking < ActiveRecord::Base
belongs_to :route
belongs_to :customer
end
class Customer < ActiveRecord::Base
has_many :bookings
has_many :routes, through => :bookings
end
귀하의 예약 모델은 날짜/위치를 유지하는 것입니다 그리고 당신은 그들에 액세스 할 수 있습니다 :
어떻게이 위치 필드에서 어떻게 작동합니까? 고객에게도 할당 할 수 있습니까? 주어진 예약 – Inez
: C = Customer.first B = c.bookings.first b.position 그러나 이미 위의 코드가 순서대로 경로를 반환합니다, 당신은 단지 부분 (이상 루프를 전달할 수 컨트롤러에서 결과 집합). – cite