Ruby on Rails - 有一个或多个关联

时间:2021-06-06 09:57:45

So I have been thinking a lot how to build associations for my app, but every time I see a model from an angle, I see it doesn't make sense or not efficient.

所以我一直在考虑如何为我的应用程序建立关联,但每次从某个角度看模型时,我都认为它没有意义或效率不高。

I have three models: Tournament, RoundRobin and Elimination. A tournament can be either a round_robin OR elimination OR both! So round_robin or/and elimination belong to a tournament.

我有三种模式:锦标赛,RoundRobin和Elimination。比赛既可以是round_robin也可以是淘汰赛,或者两者兼而有之!所以round_robin或/和淘汰属于锦标赛。

I am in a condition that the association I built is funny and doesn't make sense to me.

我的条件是我建立的协会很有趣,对我来说没有意义。

class Tournament < ActiveRecord::Base
    has_many :round_robins
    has_many :eliminations
end

class Elimination < ActiveRecord::Base
    belongs_to :tournament
end

class RoundRobin < ActiveRecord::Base
    belongs_to :tournament
end

I have another model called Match. I dunno how to place it. Should it go under Tournament or RoundRobin and Elimination?

我有另一个名为Match的模型。我不知道怎么放它。它应该参加锦标赛还是RoundRobin和Elimination?

Thanks in advance

提前致谢

2 个解决方案

#1


0  

Maybe you should have just one table Tournament with column 'kind'. For example, if kind=0 that Tournament is a RoundRobin, if kind=1 that Tournament is a Elimination, if kind=2 that Tournament is both Elimination and RoundRobin.

也许你应该只有一个桌子锦标赛,列'亲切'。例如,如果kind = 0表示锦标赛是RoundRobin,如果kind = 1表示锦标赛是淘汰赛,如果kind = 2表示锦标赛是淘汰赛和RoundRobin。

#2


0  

Edit: Since you aren't digging on STI, the alternative is having two types of Tournaments (round robin and Elimination that both belongs_to Tournament.

编辑:由于你没有挖掘STI,另一种选择是有两种类型的锦标赛(循环赛和淘汰赛都属于锦标赛。

So in that case:

所以在那种情况下:

class Tournament < ActiveRecord:Base
 has_many :matches
 #attributes specific to any type of tournament: name, date, etc.
end

class RoundRobin < ActiveRecord:Base
  belongs_to :tournament

  #adds attributes specific to round robin tournaments
end
#etc
class Elimination < ActiveRecord:Base
  belongs_to :tournament
  #has attributes specific to elimination tournaments
end

class Match 
   belongs_to :tournament
end

So if you have a round_robin tournament it can get all the basic stuff from tournament:

所以,如果你有一个round_robin锦标赛,它可以从锦标赛中获得所有基本的东西:

round_robin = RoundRobin.find(1)
#get tourney name
puts round_robin.tournament.name
puts round_robin.tournament.match.team_one_name #allowing you to access the match as well...

If you wanted to you, could then have an attribute on Tournament that is called type (that let's you know which one it is). In rails these are traditionally strings. But in other languages, we typically used lookup_tables with foreign keys.

如果你想要的话,可以在锦标赛上有一个名为type的属性(让你知道它是哪一个)。在rails中,这些传统上是字符串。但在其他语言中,我们通常使用带有外键的lookup_tables。

#to find all round_robins
tournaments = Tournament.find_by_type("round_robin")

tournmaments.each do |tourney|
  round_robin = RoundRobin.find_by_tournament_id(tourney.id)
end
#there are actually a bunch of different ways to query these:
round_robins = RoundRobin.all
tournaments = Tournament.joins(:round_robin).where("tournament_date > ?", date_lookup)
etc...

#1


0  

Maybe you should have just one table Tournament with column 'kind'. For example, if kind=0 that Tournament is a RoundRobin, if kind=1 that Tournament is a Elimination, if kind=2 that Tournament is both Elimination and RoundRobin.

也许你应该只有一个桌子锦标赛,列'亲切'。例如,如果kind = 0表示锦标赛是RoundRobin,如果kind = 1表示锦标赛是淘汰赛,如果kind = 2表示锦标赛是淘汰赛和RoundRobin。

#2


0  

Edit: Since you aren't digging on STI, the alternative is having two types of Tournaments (round robin and Elimination that both belongs_to Tournament.

编辑:由于你没有挖掘STI,另一种选择是有两种类型的锦标赛(循环赛和淘汰赛都属于锦标赛。

So in that case:

所以在那种情况下:

class Tournament < ActiveRecord:Base
 has_many :matches
 #attributes specific to any type of tournament: name, date, etc.
end

class RoundRobin < ActiveRecord:Base
  belongs_to :tournament

  #adds attributes specific to round robin tournaments
end
#etc
class Elimination < ActiveRecord:Base
  belongs_to :tournament
  #has attributes specific to elimination tournaments
end

class Match 
   belongs_to :tournament
end

So if you have a round_robin tournament it can get all the basic stuff from tournament:

所以,如果你有一个round_robin锦标赛,它可以从锦标赛中获得所有基本的东西:

round_robin = RoundRobin.find(1)
#get tourney name
puts round_robin.tournament.name
puts round_robin.tournament.match.team_one_name #allowing you to access the match as well...

If you wanted to you, could then have an attribute on Tournament that is called type (that let's you know which one it is). In rails these are traditionally strings. But in other languages, we typically used lookup_tables with foreign keys.

如果你想要的话,可以在锦标赛上有一个名为type的属性(让你知道它是哪一个)。在rails中,这些传统上是字符串。但在其他语言中,我们通常使用带有外键的lookup_tables。

#to find all round_robins
tournaments = Tournament.find_by_type("round_robin")

tournmaments.each do |tourney|
  round_robin = RoundRobin.find_by_tournament_id(tourney.id)
end
#there are actually a bunch of different ways to query these:
round_robins = RoundRobin.all
tournaments = Tournament.joins(:round_robin).where("tournament_date > ?", date_lookup)
etc...