2012-08-13 64 views
0

我有3個模型叫價格,單價和購買。 Price和UnitPrice模型有一個叫做amount的屬性,我試圖將其範圍擴展到兩者的總和。我創建了兩個範圍,一個用於兩個模型的總和。另一個範圍是獲取兩個型號的date字段的date屬性。方法給ActiveRecord ::關係錯誤?

我試圖做到這一點:

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

我應該怎麼辦?

+0

它說什麼是未定義的?當有一個時,包括錯誤消息很酷:) – 2012-08-13 21:30:56

+0

@DaveNewton在這裏你去:'# undefined方法價格' – LearningRoR 2012-08-13 22:00:59

+1

你試過「價格」而不是「self.price」嗎? – 2012-08-13 22:02:42

回答

1

totaltoday的模型對象上定義的方法。當您撥打current_user.purchases時,您將關聯到has_many,這意味着最後它是陣列。因此你不能調用購買方法。你可以這樣來做:

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

範圍可以在關係被調用。

您需要再次調用注入total是Purchase方法,關係是Array,因此您需要聚合數組。爲了保持代碼乾淨,你可能希望在User定義today_purchases_total方法,那麼你可以這樣調用它:

<%= number_to_currency(current_user.today_purchases_total) %> 

有關此的詳細信息可以參閱http://guides.rubyonrails.org/active_record_querying.html#scopes和所有的回報率一般指導。

+0

當調用'.... purchases.today.sum(&:total)...'範圍時,我得到一個ArgumentError,錯誤的參數個數(0代表1)錯誤。 – LearningRoR 2012-08-17 18:50:51

+0

可能它使用ActiveRecord的sum方法。我改變它注入 - >現在應該工作。 – 2012-08-17 18:58:23

+0

這仍然不起作用。我實際上會改變我的設計,看看我能做些什麼。欣賞所有的幫助。 – LearningRoR 2012-08-17 19:45:07

2

我認爲你的問題可能是你正在使用類方法而不是實例方法。在你購買類,方法定義之前刪除self.

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 
+0

這給了我錯誤'ActionView :: Template :: Error(今天未定義的方法爲#):'。從未見過這種類型的錯誤。這是當我這樣做時:'<%= number_to_currency(current_user.purchases.today.total)%>' – LearningRoR 2012-08-14 00:56:18

+1

當你調用'current_user.purchases.today'時,你沒有調用'Purchase'模型的方法 - 你實際上試圖在關係'User#has_many:purchases'上調用'today'方法。 你想要的是定義一個[關聯擴展](http://guides.rubyonrails.org/association_basics。HTML#協會的擴展),看起來像: 的has_many:購買高清今天 //做 ... 結束 結束 – 2012-08-14 01:06:14

+0

@AlistairIsrael這個問題:http://stackoverflow.com/questions/2187357/when-to -use-association-extensions-vs-named-scopes指出不以這種方式使用關聯擴展。你說什麼? – LearningRoR 2012-08-14 18:26:56

相關問題