1
我有一個簡單的查詢,Rails似乎是解釋爲fixnum,但我不知道爲什麼。我的代碼如下所示:Rails Fixnum錯誤
@user_with_points = Point.select("sum(points) as points, user_id").order("points desc").group("user_id")
@user_with_points.each_with_index do |user_with_point, index|
當我添加puts @user_with_points
,它表明:
#<Point:0x6360138>
#<Point:0x6322f38>
不過,我收到此錯誤此錯誤:
NoMethodError: undefined method 'each' for 75:Fixnum
將整個代碼
def self.update_overall_rank_and_points
@user_with_points = Point.select("sum(points) as points, user_id").order("points desc").group("user_id")
rank = 0
points = 0
@user_with_points.each_with_index do |user_with_point, index|
@user = User.find(user_with_point.user_id)
if user_with_point.points != points
points = user_with_point.points
rank += 1
end
@user.rank = rank
@user.points = user_with_point.points
@user.save
end
end
當我做到這一點,讓我過去的錯誤與「每一個」,但我收到我的代碼這個'如果user_id.points = points'類似的錯誤!我在上面粘貼了我的整個原始代碼和修改後的版本:https://s3.amazonaws.com/predict_images/points+fixnum+error.PNG – yellowreign 2013-03-08 04:04:51
@yellowreign,Nope,'sum'返回一個散列,'user_id'是用戶的id,它是一個整數。 – xdazz 2013-03-08 04:11:53
@yellowreign看我的編輯。 – xdazz 2013-03-08 04:12:57