class User < ActiveRecord::Base
has_many :queues
end
class Queue < ActiveRecord::Base
belongs_to :user
has_many :dvd_queues
has_many :dvds, through: :dvd_queues
end
class Dvd < ActiveRecord::Base
has_many :dvd_queues
has_many :queues, through: :dvd_queues
end
class DvdQueue < ActiveRecord::Base
belongs_to :dvd
belongs_to :queue
end
Queue
& Dvd
사이를 has_many
대신 has_and_belongs_to_many
의 연결을 사용하는 이점은 이것이 당신이 DvdQueue
에, priority
처럼 필드를 추가 할 수 있다는 것입니다. 모델 뷰와 컨트롤러를 필요
내가 사용자가 는 일반적으로
당신이 사용자에 대한 해당 컨트롤러가있는 것 자신의 큐에 DVD를 추가하고, 자신의 대기열을 볼 수 있도록하려는 경우, 대기열 및 Dvd는 표준 경로에 매핑 된 CRUD 작업을 제공하며 색인보기, 새로운 & 편집 작업을 제공합니다.
'has_many : ~ dvd_queues'를 사용하려면 어디에서나'has_many : dvd_queues'를 추가해야합니다. 왜 has_and_belongs_to_many를 사용하고 join 모델을 제거하지 않겠습니까? –
감사합니다 ... 어떻게이보기/컨트롤러에 묶어? 어떤 모델은 사용자가 큐에 DVD를 추가하고 대기열을 볼 수있게하려는 경우보기 및 컨트롤러가 필요합니다. –
멋진 답변, 감사합니다! –