露营模式忽略了关联

时间:2011-09-27 19:37:29

标签: ruby activerecord camping

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

我使用它错了吗?

1 个答案:

答案 0 :(得分:0)

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

class TeamX
  def get(name)
    @team = Team.where(:name => name).first
  end
end