Metoda daje ActiveRecord :: Błąd relacji?
Mam 3 modele o nazwie Price, UnitPrice i Purchase. Modele Price i UnitPrice mają atrybut o nazwieamount
staram się sięgnąć i uzyskać całkowitą sumę obu połączonych. Stworzyłem dwa zakresy, jeden dla całkowitej sumy obu modeli. Innym zakresem jest zdobyciedate
atrybut obu modelidate
pola.
Próbuję to zrobić:
<%= number_to_currency(current_user.purchases.today.total)
Ale popełnij błąd:
NoMethodError in pages#home
undefined method `today' for #<ActiveRecord::Relation:0x77f94c0>
Mój kod:
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
Co powinienem zrobić?