2013-07-26 1 views
1

나는 골프 관련 웹 사이트를 개설하고 있습니다. 몇 가지 모델이 있지만 코스를 통합하려고 할 때 문제가 발생했습니다. 골프 클럽에 골프 코스가 두 개 이상이거나 코스 이름이 골프 클럽과 다른 경우 코스는 코스 이름을 나타냅니다 (예 : Troon North Golf Club에는 두 개의 코스, 피나클 및 기념비가 있음). 연결을 만드는 방법을 모르겠다.활기찬 기록 계열

class Tournament < ActiveRecord::Base 
    has_many :rounds, dependent: :destroy 
    has_many :clubs, through: :rounds, dependent: :destroy 
    // do I need this to be able to do @tournament.rounds.first.course.first.name? 
    has_many :courses, through: :rounds 

class Round < ActiveRecord::Base 
    belongs_to :tournament 
    belongs_to :club 
    // not all rounds will have a course 
    has_many :courses, :through => :rounds 

class Club < ActiveRecord::Base 
    has_many :rounds, dependent: :destroy 
    has_many :tournaments, :through => :rounds, dependent: :destroy 
    has_many :club_images, dependent: :destroy 
    // not all clubs will have a course 
    has_many :courses, dependent: :destroy 

class Course < ActiveRecord::Base 
    belongs_to :club 
    belongs_to :rounds 

나는 다음을 사용하여 시도했다. 나는 그것을 생각했다 : through는 http://guides.rubyonrails.org/association_basics.html, 2.4 절을 읽은 후에 사용할 수있다.

create_table "clubs", :force => true do |t| 
    t.string "name" 
    t.string "address" 
    t.string "city" 
    t.string "state" 
    t.string "zip" 
    t.string "phone" 
    t.string "website" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.string "logo_file_name" 
    t.string "logo_content_type" 
    t.integer "logo_file_size" 
    t.datetime "logo_updated_at" 
end 

create_table "courses", :force => true do |t| 
    t.integer "club_id" 
    t.string "name" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
end 

create_table "rounds", :force => true do |t| 
    t.integer "tournament_id" 
    t.integer "club_id" 
    t.integer "course_id" 
    t.datetime "start_time" 
    t.datetime "checkin_time" 
    t.datetime "entry_deadline" 
    t.decimal "member_fee" 
    t.decimal "guest_fee" 
    t.boolean "scoring" 
    t.boolean "lunch_included" 
    t.text  "comments" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
end 

create_table "tournaments", :force => true do |t| 
    t.string "name" 
    t.date  "start_date" 
    t.date  "end_date" 
    t.text  "comments" 
    t.text  "practice_round_comments" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
end 

내가 round.courses @ 실행하려고

, 나는 다음과 같은 메시지가 얻을 - 액티브 :: HasManyThroughAssociationNotFoundError을 : 모델 라운드에서 라운드 : 연결을 찾을 수 없습니다.

나는 혼란스럽고 잘 모르겠다. 어떤 도움을 주시면 감사하겠습니다. 감사!

답변

0

belongs_to 연관은 항상 단일이어야합니다. 또한 물론 이전을 위해 round_id를 지정하지 않았습니다.

토너먼트 모델에서는 'tournament.courses'를 통해 토너먼트 코스에 직접 액세스 할 수 있습니다. '통과'를 사용하여 has_many 관계에 액세스 할 필요가 없습니다. 예를 들어

@tournament.rounds.first.courses.first.name #is wrong way 
@tournament.courses.first.name #is the correct way since you have defined a has_many relationship. 

키워드가 구현 '을 통해'내부 SQL이 가입 할 수 있습니다.

라운드 모델에서는 '코스'에 액세스 할 수 없습니다. 신고하지 않은 '라운드'를 통해 액세스하고 있기 때문입니다. 관계가 어떻게 정의되고 실제로 레일스에서 ​​어떻게 작동하는지 이해하십시오.

+0

단 하나의 오타를 보내 주셔서 감사합니다. 어쩌면 내 디자인에 결함이있는 것일 수도 있지만 코스가 실제로 클럽 설치의 일부이기 때문에 나는 round_id를 지정하지 않았습니다. 1 년에 한 번 Pinnacle 코스에서 Troon North에서 토너먼트를 개최합니다. Round_Course 테이블처럼 그들을 연결하는 다른 모델을 생성해야합니까? – Memo