我是一名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