I m 试图用一个班级方法排名以下:
@sorted_datsk_hpe = all_datsk_hpe.sort_by { |hpe| Hpe.datsk_point_count(hpe) }
下面请我:
def self.datsk_point_count(hpe)
points = 0
if hpe.gen_ac != ""
if hpe.gen_aa != nil
points += hpe.gen_aa.to_i * hpe.gen_acd
else
points += hpe.gen_amc.to_i * hpe.gen_acd
end
else
(1..10).each do |num|
if hpe["ad_c_#{num}"] != ""
if hpe["ad_c_#{num}_available"] != nil
points += hpe["ad_c_#{num}_available"].to_i * hpe["ad_c_#{num}_discount"]
else
points += hpe["ad_c_#{num}_max_capacity"].to_i * hpe["ad_c_#{num}_discount"]
end
end
end
end
points
end
我
TypeError: String can t be coerced into Integer
from app/models/hpe.rb:420:in `+
from app/models/hpe.rb:420:in `datsk_point_count
from (irb):30:in `block in irb_binding
from (irb):30:in `sort_by
from (irb):30
但是,如果单独使用班级方法的话,那是行之有效的,那么我必须做的是错误的。