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
我使用它错了吗?
答案 0 :(得分:0)
请注意,您正在ActiveRecord :: Relation的实例上调用players
。您实际上没有一个团队,您可以查询多个团队。只需将.first
附加到@team
的定义即可获得第一个团队:
class TeamX
def get(name)
@team = Team.where(:name => name).first
end
end