我正在爲我的高爾夫協會製作網站。我有幾種不同的模型,但當我試圖合併課程時遇到了問題。如果高爾夫俱樂部有多個球場或球場名稱與高爾夫俱樂部不同(例如:特倫北高爾夫俱樂部有兩個球場,品尼高和紀念碑),球場將代表球場名稱。我不知道如何創建關聯有效記錄協會
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
我試過使用:通過,也沒有它。我認爲:通過閱讀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,我得到以下信息 - 的ActiveRecord :: HasManyThroughAssociationNotFoundError:找不到關聯:兩輪模型回合。
我有點困惑,不知道我在哪裏。任何幫助,將不勝感激。謝謝!
感謝單數錯字。也許我的設計有缺陷,但我沒有在課程中指定round_id,因爲課程真的是俱樂部設置的一部分。我們每年都會在Pinnacle球場舉辦Troon North錦標賽。我應該創建另一個模型,像Round_Course表一樣鏈接它們在一起嗎? – Memo