我正在使用Postgresql,我的播放器模型有一些关联,例如:
class Goal < ActiveRecord::Base
belongs_to :player
end
class Substitution < ActiveRecord::Base
belongs_to :player
end
class Penalty < ActiveRecord::Base
belongs_to :player
end
在我看来,我列出了一个球队的阵容,在每个球员的名字后面,我想要显示他们得分的目标,他们收到的点球和他们被替换的分钟(如果有的话)。
1. Player A - goal_icon (minute) goal_icon(minute) yellow_card_icon (minute)
2. Player B - yellow_card_icon (minute)
3. Player C - goal_icon (minute) substituted (minute)
所以我显然想:
我该怎么做?
答案 0 :(得分:1)
我会实施Single Table Inheritance或Multiple Table Inheritance,然后可以致电:@player.events.sort(:minute)
。
例如STI:
class Player < AR
has_many :events
end
class Event < AR
belongs_to :player
end
class Goal < Event end
class Substitution < Event end
class Penalty < Event end
@player.events.sort(:minute) # [#Goal, #Goal, #Penalty]