I'm using ActiveRecord and Ruby (outside of Rails) to track some statistics.
I added a method (total_cost
) to one of my models to do calculations using a couple of the columns in the current model as well as a column from another model.
I'd really like to be able to use some of ActiveRecord's provisions for math (averaging, sums) and plain old finding using the method I defined, but any attempt to do so nets me (a perfectly understandable) 'Unknown column' error, e.g.
Article.find(:all, :conditions => ["total_cost > ?", 300])
As a result, I'm doing things in a manner we could describe as brute-forcey, just finding all the Articles then stuffing each one's total_value into an array and doing sums and averages with that.
Do I have any alternatives besides what I'm doing? Should I be looking past ActiveRecord to MySQL itself to calculate the values in question?