私はこのクラスとその中にこれらのメソッドを持っています。
class Purchase < ActiveRecord::Base
belongs_to :user
belongs_to :price
belongs_to :unit_price
scope :today, lambda { joins(:unit_price, :price).
where(:prices => { :date => Date.today },
:unit_prices => { :date => Date.today } ) }
# not working
def total
self.price.sum(:amount) + self.unit_price.sum(:amount)
end
end
スコープチェーンを次のように使用しようとすると、次のようになります。
<%= current_user.purchases.today.map(&:total) %>
の空の配列結果が得られます[]
。私は本当にこれをやろうとしています。
<%= number_to_currency(current_user.purchases.today.map(&:total)) %>
ただし、このエラーが発生した場合も、これは機能しません。
undefined method `to_f' for []:Array
なぜ空に戻るのですか?
ありがとう。