Web Development

When using ActiveRecord, we may think that you are too far from the real thing. It is actually very at implementing actual SQL statements.

What do you to get the SUM of multiple columns using ActiveRecord.

SELECT SUM(qhse+qhse_manager+managing_director+other_internal) FROM "stops"

This is very easy to implement with Rails ActiveRecord

ActiveRecord

@location_ytd_qhse = Stop.sum("qhse+qhse_manager+managing_director+other_internal")

This will produce our nice SQL Statement:

SQL Statement

SELECT SUM(qhse+qhse_manager+managing_director+other_internal) FROM "stops"

If you want to try something else, you can multiply column sum values:

ActiveRecord

@location_ytd_qhse = Stop.sum("qhse*qhse_manager*managing_director*other_internal")

SQL Statement

SELECT SUM(qhse*qhse_manager*managing_director*other_internal) FROM "stops"