0
我正在使用Rails 4.2.3和MySQL 5.5.37。我想寫一個finder方法爲我的模型,所以我寫了這個(./app/models/user_object.rb):在我的應用程序中定義查找器方法的問題
class UserObject < ActiveRecord::Base
validates :day, :presence => true
validates_numericality_of :total
validates :object, :presence => true
def find_total_by_user_object_and_year
UserObject.sum(:total, :conditions => ['user_id = ?', params[:user_id], 'object = ?', params[:object], 'year(day) = ?', params[:year]])
end
end
然而,當我嘗試將控制器中調用該方法,像這樣
@my_total = UserObject.find_total_by_user_object_and_year(session["user_id"], 3, @year)
我收到以下錯誤
undefined method `find_total_by_user_object_and_year' for #<Class:0x007fb7551514e0>
什麼是定義我的查找方法的正確方法?