gpt4 book ai didi

ruby-on-rails - rails 协会 - has_many 与 has_and_belongs_to_many

转载 作者:数据小太阳 更新时间:2023-10-29 08:04:30 24 4
gpt4 key购买 nike

我在处理我认为是基本关联的问题时遇到了问题。

我有一个 Game 模型和一个 Matchset 模型。

在游戏模型中是一个游戏列表。游戏只在游戏表中列出一次,但它们可以属于多个 Matchsets。

匹配集.rb -

has_many :games

对于 game.rb 我不确定我会放什么。我不想放 belongs_to 因为它属于许多匹配集,而不仅仅是一个。而且我认为我不想将 has_and_belongs_to_many 放在一起,因为匹配集不一定“属于”游戏,但也许我只是看错了。

示例:匹配组 1 包含游戏 1、3 和 5。匹配组 2 包含游戏 2 和 3。匹配组 3 包含游戏 3、4 和 5。

我的 Oracle SQL 背景和在我脑海中的 Matchset 表看起来像这样。

id | game_id 
1 | 1
1 | 3
1 | 5
2 | 2
2 | 3
3 | 3
3 | 4
3 | 5

感谢任何帮助。

最佳答案

这些关系应该适合你:

class Game < ActiveRecord::Base
has_many :game_match_set_relations
has_many :match_sets, through: :game_match_set_relations

class MatchSet < ActiveRecord::Base
has_many :game_match_set_relations
has_many :games, through: :game_match_set_relations

class GameMatchSetRelation < ActiveRecord::Base
belongs_to :game
belongs_to :match_set

validates :game_id, presence: true
validates :match_set_id, presence: true

关于ruby-on-rails - rails 协会 - has_many 与 has_and_belongs_to_many,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19986237/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com