开发者

Camping model ignores association

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

  class Player < Base
    belongs_to :team
  end

When calling @team.players (or @player.team):

NoMethodError at /team/red
un开发者_运维问答defined method `players' for [#<App::Models::Team (...)>]:ActiveRecord::Relation

Am I using it wrong?


Note that you're calling players on an instance of ActiveRecord::Relation. You don't actually have one Team, you have a query of several teams. Simply append .first to your definition of @team to get the first team:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜