I’m trying to get some stats from my database about my model, and I’m
not sure how to go about it.
If I have a model ‘foo’ that has an attribute ‘color’ and I want to
get some counts on this I could use the sql:
SELECT color, COUNT(*) AS count
FROM items
GROUP BY status
which would return something like:
±-------±------+
| color | count |
±-------±------+
| blue | 3 |
| green | 1 |
| orange | 1 |
| red | 4 |
±-------±------+
How to translate this into:
def stats
return a structured result I can iterate over in a view
somewhere…
like and array of hashes: {[:color => ‘blue’, :count => 3],
[:color => ‘green’, :count => 1]…}
end
The usual Model.find_by_sql wouldn’t work as I’m not returning a
model, and I’m not seeing anything jump out at me like
ActiveRecord::Base::Connection.execute_sql for doing this sort of
arbitrary sql execution.
Any suggestions? Am I missing something really obvious?
–
Craig B.
AIM: kreiggers