2016-12-19 2 views
0

Я пытаюсь вернуться в Ruby on Rails, но у меня есть вопрос, который казался простым. Мое приложение основано на турнирах по гольфу, в которых каждый турнир может иметь 1 или более раундов гольфа. Каждый раунд гольфа проводится по 1 курсу.Ассоциации Помощь для newby

Я создал следующие ассоциации:

class Course < ApplicationRecord 
    has_many :rounds, dependent: :destroy 
    has_many :tournaments, :through => :rounds, dependent: :destroy 
    has_attached_file :logo 
end 

class Tournament < ApplicationRecord 
    has_many :rounds, dependent: :destroy 
    has_many :courses, through: :rounds, dependent: :destroy 
end 

class Round < ApplicationRecord 
    belongs_to :tournament 
    has_one :course, dependent: :destroy 
end 

можно выполнить следующие действия:

- tournament.rounds 
- tournament.rounds[0] or tournament.rounds[1] 
- course.tournaments 
- course.rounds 

Я думал, что я должен быть в состоянии сделать следующее

- tournament.courses 

Кроме того, курс [0]. Кажется, что товары возвращают повторяющиеся данные, как если бы два турнира были связаны с курсами потому что у меня два раунда.

=> #<ActiveRecord::Associations::CollectionProxy [#<Tournament id: 1, name: "December", start_date: "2016-12-20", end_date: "2016-12-20", comments: "", practice_round_comments: "", created_at: "2016-12-19 18:57:25", updated_at: "2016-12-19 18:57:25">, #<Tournament id: 1, name: "December", start_date: "2016-12-20", end_date: "2016-12-20", comments: "", practice_round_comments: "", created_at: "2016-12-19 18:57:25", updated_at: "2016-12-19 18:57:25">] 

Моя миграция выглядеть следующим образом:

class CreateRounds < ActiveRecord::Migration[5.0] 
    def change 
    create_table :rounds do |t| 
     t.belongs_to :tournament, index: true 
     t.belongs_to :course, index: true 
     t.datetime :start_time, :null => false 
     t.datetime :checkin_time, :null => false 
     t.datetime :entry_deadline, :null => false 
     t.decimal :member_fee, :precision => 6, :scale => 2, :default => 65.00 
     t.decimal :guest_fee, :precision => 6, :scale => 2, :default => 75.00 
     t.boolean :scoring, :default => true 
     t.boolean :lunch_included, :default => false 
     t.text :comments, :null => true 

     t.timestamps 
    end 
    end 
end 

class CreateTournaments < ActiveRecord::Migration[5.0] 
    def change 
    create_table :tournaments do |t| 
     t.string :name, :null => false 
     t.date :start_date, :null => false 
     t.date :end_date, :null => false 
     t.text :comments, :null => true 
     t.text :practice_round_comments, :null => true 

     t.timestamps 
    end 
    end 
end 

class CreateCourses < ActiveRecord::Migration[5.0] 
    def change 
    create_table :courses do |t| 
     t.string :name, :limit => 30, :null => false 
     t.string :address, :limit => 30, :null => false 
     t.string :city, :limit => 30, :null => false 
     t.string :state, :limit => 2, :null => false 
     t.string :zip, :limit => 9, :null => false 
     t.string :phone, :limit => 10, :null => false 
     t.string :website, :limit => 100, :null => true 
     t.attachment :logo 

     t.timestamps 
    end 
    end 
end 

ответ

1

Ваш раунд указывает, что турнир has_one :course и должен быть belongs_to, чтобы соответствовать перенастройки

Смежные вопросы