Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aggregate object attribute #2

Closed
rmeissner opened this issue Jul 18, 2013 · 1 comment
Closed

Aggregate object attribute #2

rmeissner opened this issue Jul 18, 2013 · 1 comment

Comments

@rmeissner
Copy link

I would like to sum up an attribute of all entries in a table.

e.g.: the age of all persons in a database.

normal approach: Person.sum('age')

ACFS apprach:

sum = 0
Person.all do | person |
sum = sum + person.age
end
Acfs.run

Is it possible to have a better way for that?

@jgraichen
Copy link
Owner

Acfs is for resource oriented access on services. So I would prefer you create a resource representing your sum (or multiple stats about one person) in the source service. Not all resources must be database models.

I would create a resource Statistic in the service providing Person with fields for your summed age:

MyService::Statistic.find 1 do |stats|  # Would be nice to have singleton resources
  puts stats.accumulated_age
end

This allows you to implement the business logic in your service. It would be easy to change the algorithm, cache results etc. without changing the caller app. My thumb rule is: "If it contains an operator (+) it's business logic and must be implemented in the service (and represented as a resource)."

As I commented above it would be nice to have singleton resources (GET /resource instead of GET /resources/:id) as the statistic resource should be singleton and only exists at one place. I will add this to feature list but you can emulate this by choosing a fix id.

Hope that helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants