我有 3 个模型,称为价格、单价和购买。Price 和 UnitPrice 模型有一个名为的属性amount
,我试图将其限定并获取两者的总和。我创建了两个范围,一个用于两个模型的总和。另一个范围是获取date
两个模型date
字段的属性。
我正在尝试这样做:
<%= number_to_currency(current_user.purchases.today.total)
但得到错误:
NoMethodError in pages#home
undefined method `today' for #<ActiveRecord::Relation:0x77f94c0>
我的代码:
class Purchase < ActiveRecord::Base
belongs_to :user
belongs_to :price
belongs_to :unit_price
def total
self.price.sum(:amount) + self.unit_price.sum(:amount)
end
def today
self.price.where(:date => Date.today) && self.unit_price.where(:date=> Date.today)
end
end
class Price < ActiveRecord::Base
attr_accessible :amount, :date
belongs_to :user
has_many :purchases
end
class UnitPrice < ActiveRecord::Base
attr_accessible :amount, :date
belongs_to :user
has_many :purchases
end
我该怎么办?