Rails中通过关系2转1 has_many(如何将三个模型连接在一起)

发布于 2024-12-24 03:54:09 字数 1082 浏览 2 评论 0原文

连接以下三个模型的最佳方式是什么?

class Tournament < ActiveRecord::Base
    has_many :submissions
    has_many :creatures, :through => :submissions, :uniq => true
    has_many :teams, :through => :submissions, :uniq => true
end

class Creature < ActiveRecord::Base  
    belongs_to :team
    has_many :tournaments, :through => :team
end

class Submission < ActiveRecord::Base
    belongs_to :tournament
    belongs_to :team
end

class Team < ActiveRecord::Base
    has_many :creatures
    has_many :submissions
    has_many :tournaments, :through => :submissions
end

我想要实现这样的目标:

> team_1.tournaments[0] = tournament_1

> tournament_1.teams[0]
 (returns team_1)

> team_1.tournaments[0].creatures
 (returns [])

> team.tournaments[0].creatures[0] = creature_1

> creature_1.tournaments 
 (returns tournament_1)

让特定生物和团队与特定锦标赛相关联的最有效方法是什么?

编辑:以上是期望的行为..当前的问题是,一旦我将团队添加到tournament.teams中,该团队中的所有生物都会自动将该锦标赛列在creature.tournament中,而我正在尝试使生物有选择地添加到锦标赛中。一张连接表是否有可能?

谢谢!

What would be the best way to connect following three models?

class Tournament < ActiveRecord::Base
    has_many :submissions
    has_many :creatures, :through => :submissions, :uniq => true
    has_many :teams, :through => :submissions, :uniq => true
end

class Creature < ActiveRecord::Base  
    belongs_to :team
    has_many :tournaments, :through => :team
end

class Submission < ActiveRecord::Base
    belongs_to :tournament
    belongs_to :team
end

class Team < ActiveRecord::Base
    has_many :creatures
    has_many :submissions
    has_many :tournaments, :through => :submissions
end

I want to achieve something like this:

> team_1.tournaments[0] = tournament_1

> tournament_1.teams[0]
 (returns team_1)

> team_1.tournaments[0].creatures
 (returns [])

> team.tournaments[0].creatures[0] = creature_1

> creature_1.tournaments 
 (returns tournament_1)

What is the most efficient way to have a specific creature and a team associated with a specific tournament?

EDIT: The above is the desired behavior.. Current problem is that as soon as I add team to tournament.teams all the creatures in that team automatically have that tournament listed in creature.tournament, while I am trying to make it so that creatures are added to tournament selectively.. Is it at all possible with one join table?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居者 2024-12-31 03:54:09

提交应该是锦标赛团队之间的连接表,对吗?

             Creature [id, team_id]
                             |
                             |
                             |
                       Team [id]
                             |
                             |
                             |
           Submission [id, team_id, tournament_id]  
                                          |
                                          |
                                          |
                              Tournament [id]

模型关系:

class Creature < ActiveRecord::Base  
  belongs_to :team
  has_many :tournaments, :through => :team   # this should work since Rails 3.1 or 3.2
end

class Team < ActiveRecord::Base  
  has_many :creatures 
  has_many :tournaments, :through => :submissions
  has_many :submissions
end

class Submission < ActiveRecord::Base  
  belongs_to :team
  belongs_to :tournament
end

class Tournament < ActiveRecord::Base  
  has_many :teams, :through => :submissions
  has_many :creatures, :through => :teams     # this should work since Rails 3.1 or 3.2
  has_many :submissions
end

现在您应该能够调用:
team_1.tournaments[0].creatures

Submission should be your join table between Tournamentsand Teams, correct?

             Creature [id, team_id]
                             |
                             |
                             |
                       Team [id]
                             |
                             |
                             |
           Submission [id, team_id, tournament_id]  
                                          |
                                          |
                                          |
                              Tournament [id]

Model relationships:

class Creature < ActiveRecord::Base  
  belongs_to :team
  has_many :tournaments, :through => :team   # this should work since Rails 3.1 or 3.2
end

class Team < ActiveRecord::Base  
  has_many :creatures 
  has_many :tournaments, :through => :submissions
  has_many :submissions
end

class Submission < ActiveRecord::Base  
  belongs_to :team
  belongs_to :tournament
end

class Tournament < ActiveRecord::Base  
  has_many :teams, :through => :submissions
  has_many :creatures, :through => :teams     # this should work since Rails 3.1 or 3.2
  has_many :submissions
end

Now you should be able to call:
team_1.tournaments[0].creatures

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文