Ruby on rails RubyonRails中的循环依赖项错误
我有以下型号:Ruby on rails RubyonRails中的循环依赖项错误,ruby-on-rails,ruby,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, t
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
我得到了错误:TeamPlayerIndex中的RuntimeError:自动加载常量播放器时检测到循环依赖项
我想知道这意味着什么以及如何修复它。我已经将我的rails从4.0.2降级到了4.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
你能发布你的玩家模型的所有代码吗?或者是这样吗?我更新了我的玩家模型,所以是的,这是我确定的,现在已经修复了我的错误:SQLite3::SQLException:没有这样的列:teamplayers.live\u player\u id:SELECT teamplayers.*从teamplayers内部加入players上的玩家。id=teamplayers.player\u id,当尝试运行这一行时:为什么。我知道它要查找的列不存在,但为什么它要查找TeamPlayer。player_id是因为属于:player需要TeamPlayer表中的player_id列。有关多对多关系的示例,请参见创建迁移文件来处理多对多关系?我需要在文件中完全定义我的表,还是只定义需要关联的字段?这取决于您的数据库模式现在是什么样的-如果您已经创建了表,就不必再这样做了。如果没有,那么您显然必须在迁移中创建它们。我已经创建了表,但是没有TeamPlayer。player_id我是否只需要添加该列或重做表,我可以在迁移文件中这样做,也不介意