2017-03-01 37 views
0

Rails新手在這裏。我正在嘗試創建一個應用程序來顯示有關足球比賽的信息。我有一個Game模型,其目的是包含有關比賽的信息。我希望在對象中包含的一種類型的信息是比賽中發生的事件,例如目標和紀律處分。Rails - 像對象建模

class Game < ApplicationRecord 
    has_many :events 
end 

模擬這些事件的最佳方式是什麼?如果有隻是一個Event模式或有任何優勢,創建多個模型擴展EventGoalYellowCardRedCard等?

回答

1

您可以使用類似的事件類型模型:

# game.rb 
class Game < ApplicationRecord 
    has_many :events 
end 

# event.rb 
class Event < ApplicationRecord 
    belongs_to :event_type 
end 

# event_type.rb 
class EventType < ApplicationRecord 
end 

events表,你可以存儲信息,例如時間/筆記,並會有一個場event_type_id。在event_types表中,可以存儲諸如目標的行動,yellow_card等

然後,您就可以輕鬆地進行查詢,如發現在一個特定的匹配等

+0

沒想到這一點,但我喜歡這個主意。有沒有什麼好的方法來說明一個事件對象需要依賴於它所屬的'event_type'的不同屬性? – Jay

-1

一個建議,讓你開始的所有目標。

class Game < ActiveRecord::Base 
    has_many :teams 
    has_many :players, through: :teams 
    has_many :goals 
    has_many :cards 
end 

class Team < ActiveRecord::Base 
    has_many :players 
end 

class Player < ActiveRecord::Base 
    belongs_to :team 
    has_many :cards 
    has_many :goals 
end 

class Card < ActiveRecord::Base 
    belongs_to :player 
    belongs_to :game 
end 

class Goal < ActiveRecord::Base 
    belongs_to :player 
    belongs_to :game 
end 

* OBS:您可能需要添加一個TeamLineup模型,作爲一個團隊,可以有根據遊戲不同的陣容。我知道你問的事件,但我認爲上述提議的解決方案,模特足球比賽更好