通过模型方法和继承求和
Sum by model method and inheritance
我有 3 个模型,User、Applicant 和 ApplicantCommission。
user has_many applicants
applicant has many applicant_commissions
我想 return sum applicant_commission 实例方法到 育儿模型 。因此 @user.getTotalCommission
将 return 每个申请人的所有 applicant_commission 的总和。 @applicant.getTotalCommission
将 return 属于该申请人的每个佣金,而 @applicant_commission.getTotalCommission
将 return 仅一种佣金类型的总价值。
在ApplicantCommission.rb中我有一个实例方法:
# Returns the full amount of commission that the post has earned from this commission group.
def getTotalCommission
#Does some calculations
return number_with_precision(total.round(2), :precision => 2)
end
Applicant.rb
def getTotalCommission
self.applicant_commissions.to_a.sum(&:getTotalCommission)
end
User.rb
def getTotalCommission
self.applicants.to_a.sum(&:getTotalCommission)
end
目前,如果我有 2 个申请人佣金,一个 12.20,另一个 10.00,我得到 12.2010.00。所需的输出将是 22.20.
它应该基于简单的继承..所以也许我完全走错了路?
谢谢
getTotalCommission
方法有问题,试试这个:
def getTotalCommission
# becase number_with_precision returns string.
number_with_precision(total.round(2), :precision => 2).to_f
end
希望对您有所帮助。
您可能需要修改 User#getTotalCommission
def getTotalCommission
number_with_precision(applicants.to_a.sum(&:getTotalCommission), precision: 2).to_f
end
我认为问题出在您对 number_with_precision
的使用,这是一种在视图中使用的辅助方法,可以格式化您的数字以便显示。它 returns 一个字符串。 Rails 还提供了 sum
方法,可以将数组中的所有内容相加。
本质上你得到一个数组 ["12.20", "10.00"]
然后它通过 "12.20" + "10.00"
得到 "summed"
我会尽量将您的总佣金保留为数字,并且仅在显示时使用 number_with_precision
进行格式化。
如果您在 ApplicantComission
中的 getTotalCommission
方法只是:
def getTotalCommission
total.round(2)
end
那么您的求和代码将按预期工作。
P.S。我想问你是否也需要在那个时候四舍五入——你可能只需要在输出值时四舍五入
P.P.S。您实际上并没有进行继承,即不同的 类 相互继承。你的方法都有相同的接口,因为它们都有一个 getTotalCommission
方法,但它不是通过继承。
我有 3 个模型,User、Applicant 和 ApplicantCommission。
user has_many applicants
applicant has many applicant_commissions
我想 return sum applicant_commission 实例方法到 育儿模型 。因此 @user.getTotalCommission
将 return 每个申请人的所有 applicant_commission 的总和。 @applicant.getTotalCommission
将 return 属于该申请人的每个佣金,而 @applicant_commission.getTotalCommission
将 return 仅一种佣金类型的总价值。
在ApplicantCommission.rb中我有一个实例方法:
# Returns the full amount of commission that the post has earned from this commission group.
def getTotalCommission
#Does some calculations
return number_with_precision(total.round(2), :precision => 2)
end
Applicant.rb
def getTotalCommission
self.applicant_commissions.to_a.sum(&:getTotalCommission)
end
User.rb
def getTotalCommission
self.applicants.to_a.sum(&:getTotalCommission)
end
目前,如果我有 2 个申请人佣金,一个 12.20,另一个 10.00,我得到 12.2010.00。所需的输出将是 22.20.
它应该基于简单的继承..所以也许我完全走错了路?
谢谢
getTotalCommission
方法有问题,试试这个:
def getTotalCommission
# becase number_with_precision returns string.
number_with_precision(total.round(2), :precision => 2).to_f
end
希望对您有所帮助。
您可能需要修改 User#getTotalCommission
def getTotalCommission
number_with_precision(applicants.to_a.sum(&:getTotalCommission), precision: 2).to_f
end
我认为问题出在您对 number_with_precision
的使用,这是一种在视图中使用的辅助方法,可以格式化您的数字以便显示。它 returns 一个字符串。 Rails 还提供了 sum
方法,可以将数组中的所有内容相加。
本质上你得到一个数组 ["12.20", "10.00"]
然后它通过 "12.20" + "10.00"
我会尽量将您的总佣金保留为数字,并且仅在显示时使用 number_with_precision
进行格式化。
如果您在 ApplicantComission
中的 getTotalCommission
方法只是:
def getTotalCommission
total.round(2)
end
那么您的求和代码将按预期工作。
P.S。我想问你是否也需要在那个时候四舍五入——你可能只需要在输出值时四舍五入
P.P.S。您实际上并没有进行继承,即不同的 类 相互继承。你的方法都有相同的接口,因为它们都有一个 getTotalCommission
方法,但它不是通过继承。