方法给出 ActiveRecord::Relation 错误? [英] Method gives ActiveRecord::Relation error?

查看:45
本文介绍了方法给出 ActiveRecord::Relation 错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个模型,分别是 Price、UnitPrice 和 Purchase.Price 和 UnitPrice 模型有一个名为 amount 的属性,我试图将其限定为范围并获得两者的总和.我创建了两个范围,一个是两个模型的总和.另一个作用域是获取两个模型的 date 字段的 date 属性.

I have 3 models called Price, UnitPrice and Purchase. The Price and UnitPrice models have an attribute called amount that I'm trying to scope to and get the total sum of both combined. I created two scopes, one for the total sum of both models. The other scope is to get the date attribute of both model's date fields.

我正在尝试这样做:

<%= 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

我该怎么办?

推荐答案

totaltoday 方法是在模型对象上定义的.当您调用 current_user.purchases 时,您将关联到一个关系,即 has_many,这意味着最终它是数组.因此,您不能对其调用 Purchase 方法.你可以这样做:

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

然后这样称呼它:

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

可以在关系上调用作用域.

Scope can be called on a relation.

您需要再次调用注入,因为 total 是购买方法并且关系是数组,因此您需要聚合数组.为了保持代码干净,您可能需要在 User 上定义一个 today_purchases_total 方法,这样您就可以像这样调用它:

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) %>

有关这方面的更多信息,您可以参考 http://guides.rubyonrails.org/active_record_querying.html#scopes 和所有 RoR 指南.

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

这篇关于方法给出 ActiveRecord::Relation 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆