Ruby on Rails中的循环依赖性错误

时间:2014-01-30 22:22:03

标签: ruby-on-rails ruby

我有以下型号:

class Player < ActiveRecord::Base
has_and_belongs_to_many :fteams
end

class TeamPlayer < ActiveRecord::Base
  belongs_to :player
  belongs_to :team
end

class Team < ActiveRecord::Base
  has_many :team_players
  has_many :players, through: :team_players
end

我得到错误:Teamplayers中的RuntimeError #index:自动加载常量播放器时检测到循环依赖。

我想知道它意味着什么,以及如何解决它。我已经将我的轨道从4.0.2降级到4.0.0并且我仍然遇到同样的问题,这似乎是一个简单的解决方案,但它令人沮丧,在网上搜索并且没有任何结果

1 个答案:

答案 0 :(得分:0)

你正在寻找多对多关系吗?就像在,一个球员可以成为许多球队的一部分,球队可以有很多球员?尝试这样的事情:

class Player < ActiveRecord::Base
  has_many :team_players
  has_many :teams, through: :team_players
end

class TeamPlayer < ActiveRecord::Base
  belongs_to :player
  belongs_to :team
end

class Team < ActiveRecord::Base
  has_many :team_players
  has_many :players, through: :team_players
end