I am having what I think is a simple problem chaining two scopes (Rails 3.2.5).
I have a model called Point
with fields amount
and transaction_date
, amongst other. Users get an amount of points for various activities, and they are "available" until used, which happens as part of a transaction, at which point transaction_date
is updated, and no longer empty.
So I have a scope like this:
scope :available, where("transaction_date IS NULL OR transaction_date = ''")
and it works great, returning the proper collection of Point objects. So I can do
> Point.available
=> [#<Point id: 123, amount: 22, transaction_date: nil >][#<Point id: 456, amount: 33, transaction_date: nil >]
And if I want to know the sum of available points, I can do
> Point.available.sum("amount")
=> 55
But if I try to make another scope like
scope :available, where("transaction_date IS NULL OR transaction_date = ''")
scope :total_available, available.sum("amount")
I get the error
NoMethodError: undefined method `default_scoped?' for 22:Fixnum
or if I change the scope so it's sum("amount").available
I get the error
NoMethodError: undefined method `available' for 55:Fixnum
I can also make the :total_available
scope work by adding the condition defined in :available
, but that's not very DRY.
What am I missing here?