在我的Rails 3.2.8应用程序中,我试图计算所有的用户产品和税额。它应该得到产品的总和,然后是每个产品的税。如何获得父协会的总和?
class Product
attr_accessible :amount, :location_id, :tax_id
belongs_to :tax
belongs_to :location
belongs_to :user
belongs_to :tax, :class_name => "Tax"
def self.total
self.sum(:amount) + self.tax.sum(:amount)
end
end
Tax.rb
class Tax < ActiveRecord::Base
attr_accessible :amount, :date, :location_id
belongs_to :user
belongs_to :location
has_many :products
end
所以,当我尝试这样一个范围:
<%= number_to_currency(current_user.products.total) %>
这当然给我一个错误:
undefined method `tax' for #<Class:0x57f5298>
H我可以写这个让它工作吗?
谢谢。
您正在混合类和实例属性/方法 – apneadiving
为什么您将税作为相关对象?为什么不把税作为产品的属性? – Andrew
您可以发布相关的税务类来源吗? –