0
module App::Models
  class Team < Base
    has_many :players
    [...]
  end

  class Player < Base
    belongs_to :team
  end

调用@team.players(或@player.team)时:

NoMethodError at /team/red
undefined method `players' for [#<App::Models::Team (...)>]:ActiveRecord::Relation

我用错了吗?

4

1 回答 1

0

请注意,您正在调用playersActiveRecord::Relation 的实例。你实际上没有一个团队,你有几个团队的查询。只需附加.first到您的定义@team即可获得第一支球队:

class TeamX
  def get(name)
    @team = Team.where(:name => name).first
  end
end
于 2011-09-28T08:26:07.650 回答