开发者

Camping model ignores association

开发者 https://www.devze.com 2023-04-08 16:00 出处:网络
module App::Models class Team < Base has_many :players [...] end class Player < Base belongs_to :team
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

精彩评论

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

关注公众号