Method gives ActiveRecord::Relation error?

瘦欲@ 提交于 2019-12-02 12:40:30

The methods total and today are defined on a model object. When you call current_user.purchases you associate to a relation which is has_many which means that in the end it's the Array. Therefore you can't call Purchase methods on it. You can do it this way:

  class Purchase < ActiveRecord::Base
    # ...
    scope :today, lambda { joins(:unit_price, :price).
                             where(:price => {:date => Date.today}, 
                                   :unit_price => { :date => Date.today }) }
    def total
        self.price.sum(:amount) + self.unit_price.sum(:amount)
    end
  end

And then call it like this:

   <%= number_to_currency(current_user.purchases.today.inject{ |sum, p| sum + p.total }) %>

Scope can be called on a relation.

You need to call inject since again total is Purchase method and the relation is Array so you need to aggregate the array. In order to keep the code clean you may want to define a today_purchases_total method on User so then you can call it like:

   <%= number_to_currency(current_user.today_purchases_total) %>

For more info about this you may refer to http://guides.rubyonrails.org/active_record_querying.html#scopes and all RoR guides in general.

I think your problem may be that you're using class methods instead of instance methods. In your Purchase class, remove the self. before the method definitions:

class Purchase < ActiveRecord::Base
  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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!