私の 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>
これを機能させるにはどうすればよいですか?
ありがとうございました。