私は次のモデルを持っています
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
これらは、players_to_teams テーブルを介して結合されます。
class PlayersToTeam < ActiveRecord::Base
belongs_to :player
belongs_to :team
end
players_to_teams
という列がありますBT
。この列にアクセスできません。
これは失敗します:
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")
オプションを使用せずに Rails 3.2.1 の結合テーブルに格納されている値にアクセスする方法はありselect
ますか?
ありがとう