I have the following models:
class Client < ActiveRecord::Base
has_many :accounts, dependent: :destroy
end
class Account < ActiveRecord::Base
belongs_to :client
has_many :operations, dependent: :destroy
end
class Operation < ActiveRecord::Base
belongs_to :account
end
I manage to extract clients and all their operations grouped by client
as
follows:
scope :operations_by_client, joins(:client,
:operations).select(‘clients.firstname, clients.lastname,
sum(operations.total) as total’).group(‘clients.id, clients.firstname,
clients.lastname’).order(‘clients.lastname’)
Now how to tune/change it in case if I don’t need to calculate SUM of
the
operations but to use a special method that would calculate the sum of
all
the operations for the specified client? I mean that I should exclude
certain total values from the calculation depending on the operation
type.
Thanks and regards