我是一名rails初学者,并试图在模型中添加一些代码。下面的代码是一个例子。rails迭代方法数组undefined方法
查看:
Player_stats: <%= @player.player_pass_completion_ratio %>
型号:
class Player < ActiveRecord::Base
has_many :lefthandstats
has_many :righthandstats
def player_pass_completion_ratio
Hands = [ lefthandstats, righthandstats] #These are objects & calling @player.lefthandstats.find_with_passes directly generally works
if self.category == "Hands"
total_usual_passes = 500
Hands.each do |cmethod|
if self.cmethod.find_with_passes(:passes, :first, {:conditions => 'passes>200' }) then accuratestats += 1 end
end
end
accuracy = (accuratestats/total_usual_passes)*100
end
我得到一个未定义的方法 “cmethod” 当我尝试调用视图中的代码。任何意见是极大的赞赏。
在该方法的第一行,这似乎是说,你要在一个名为Hands的数组中放置两个局部变量名称'lefthandstats'和'righthandstats'。但是,由于这是该方法的第一行,因此这两个变量尚未定义,因此它们为零。 – GSP 2013-02-27 14:19:34