Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails-类似对象的建模_Ruby On Rails_Oop_Model - Fatal编程技术网

Ruby on rails Rails-类似对象的建模

Ruby on rails Rails-类似对象的建模,ruby-on-rails,oop,model,Ruby On Rails,Oop,Model,这里是新手。我正在尝试创建一个应用程序来显示有关足球比赛的信息。我有一个游戏模型,它包含关于比赛的信息。我想在游戏对象中包含的一类信息是比赛中发生的事件,例如进球和纪律处分 class Game < ApplicationRecord has_many :events end 课堂游戏

这里是新手。我正在尝试创建一个应用程序来显示有关足球比赛的信息。我有一个
游戏
模型,它包含关于比赛的信息。我想在
游戏
对象中包含的一类信息是比赛中发生的事件,例如进球和纪律处分

class Game < ApplicationRecord
  has_many :events
end
课堂游戏

模拟这些事件的最佳方式是什么?是否应该只有一个
事件
模型,或者创建扩展
事件
的多个模型(例如
目标
黄卡
红卡
)是否有任何好处?

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

# 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
#game.rb
类游戏<应用记录
你参加过很多活动吗
结束
#event.rb
类事件<应用程序记录
属于:事件类型
结束
#事件类型.rb
类EventType
events
表中,您可以存储时间/注释等信息,并且会有一个字段
event\u type\u id
。在
事件类型
表中,您可以存储目标、黄牌等操作


然后,您就可以轻松地进行查询,例如查找特定比赛中的所有目标等。

一个建议可以帮助您开始

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:您可能需要添加一个团队阵容模型,因为一个团队可以根据游戏拥有不同的阵容。我知道你问了一些事件,但我认为上面提出的解决方案更好地模拟了足球比赛

我没有想到这一点,但我喜欢这个想法。对于
事件
对象需要依赖于它所属的
事件类型
的不同属性,有没有什么好办法来解释?