我得到這個奇怪的錯誤。我已經定義了一個方法,我的模型Rails方法不能在查看
class Rating < ActiveRecord::Base
[...]
belongs_to :user
belongs_to :movie
[...]
def to_text
texto = case self.grade
when 0..1 then "horrible"
when 2..3 then "bad"
when 4..5 then "not bad"
when 6..7 then "good"
when 8..9 then "very good"
when 10 then "master piece"
end
end
end
然後,我的控制器上,我定義這個實例:
@[email protected](:user_id => current_user)
而且它找到它,所以它的工作原理。但是,當我調用該方法,或類似
<%= @current_user_rating.grade %>
<%= @current_user_rating.to_text %>
屬性我得到這個錯誤
undefined method `grade' for []:ActiveRecord::Relation
undefined method `to_text' for []:ActiveRecord::Relation
爲什麼變量未表現爲與相應的屬性和方法,但作爲一個關係的實例?
它確實在控制檯上工作,而不是在服務器上...
+1,我認爲你是正確的。除了打字錯誤(在'each`調用之前缺少'。');) – 2011-01-26 01:10:46