Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 连接表Rails的访问列_Ruby On Rails_Activerecord - Fatal编程技术网

Ruby on rails 连接表Rails的访问列

Ruby on rails 连接表Rails的访问列,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我有以下型号 class Player < ActiveRecord::Base has_many :players_to_teams has_many :teams, through: :players_to_teams end class Team < ActiveRecord::Base has_many :players_to_teams has_many :players, through: :players_to_teams belongs_to :

我有以下型号

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

class Team < ActiveRecord::Base
  has_many :players_to_teams
  has_many :players, through: :players_to_teams
  belongs_to :account
end
出现此错误时:

NoMethodError:   undefined method `BT' for #<ActiveRecord::Relation:0x007fd52f170c58>
PlayersToTeam Load (332.2ms)    from /Users/Tyler/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/relation/delegation.rb:45:in `method_missing'
  SELECT `players_to_teams`.* FROM `players_to_teams` WHERE `players_to_teams`.`player_id` = 13
    from /Users/Tyler/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/associations/collection_proxy.rb:101:in `method_missing'
    from (irb):4
    from /Users/Tyler/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start'
    from /Users/Tyler/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start'
    from /Users/Tyler/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands.rb:41:in `<top (required)>'
    from /Users/Tyler/Development/Rails/csbb/script/rails:6:in `require'
    from /Users/Tyler/Development/Rails/csbb/script/rails:6:in `<top (required)>'
    from -e:1:in `load'
    from -e:1:in `<main>'
有没有一种方法可以访问Rails 3.2.1中连接表中存储的值而不使用
select
选项


谢谢

因为玩家对团队是一个连接表,你没有在上面运行所有的操作,所以你得到了一个关系

首先,您需要为集合调用all或为单个PlayerToteam对象调用first


另外,我很确定它会因为大写字母(Ruby中的常量有大写字母)而阻塞名为BT的方法。

一旦我调用了“first”,一切都很好。它没有因为资本化而窒息(加分)。这是一个我正在转换的遗留数据库,所以我一定会继续。谢谢
player = Player.find(13)
player.players_to_teams.BT
NoMethodError:   undefined method `BT' for #<ActiveRecord::Relation:0x007fd52f170c58>
PlayersToTeam Load (332.2ms)    from /Users/Tyler/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/relation/delegation.rb:45:in `method_missing'
  SELECT `players_to_teams`.* FROM `players_to_teams` WHERE `players_to_teams`.`player_id` = 13
    from /Users/Tyler/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/associations/collection_proxy.rb:101:in `method_missing'
    from (irb):4
    from /Users/Tyler/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start'
    from /Users/Tyler/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start'
    from /Users/Tyler/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands.rb:41:in `<top (required)>'
    from /Users/Tyler/Development/Rails/csbb/script/rails:6:in `require'
    from /Users/Tyler/Development/Rails/csbb/script/rails:6:in `<top (required)>'
    from -e:1:in `load'
    from -e:1:in `<main>'
player.players_to_teams.select("BT")