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
精彩评论