In the view code, he maps the last three weeks worth of dates to the result of
Order.total_on(date)
The method Order::total_on
calls where
, which is an ActiveRecord query method that adds conditions to the query (see http://guides.rubyonrails.org/active_record_querying.html#conditions).
Since in the context of the Order::total_on
method, self
refers to the Order
class, what we're actually doing here is the query:
Order.where("date(purchased_at) = ?", date)
Now that actually returns an ActiveRecord::Relation
object that allows you to chain more query methods on it before executing the query. We then call #sum
on that relation (http://api.rubyonrails.org/classes/ActiveRecord/Calculations.html#method-i-sum) to further convert the query. The generated SQL would look something like:
SELECT SUM(orders.total_price) AS sum_id FROM orders WHERE (date(purchased_at) = #{date});
It's a bit hard to guess what the specific point of confusion here is, so if that's still unclear please comment.
(Edit in case I'm assuming too much: The actual connection between the Order
class and the orders
table in the database is made with the very first line of app/models/order.rb
, where the class inherits from ActiveRecord::Base
. ActiveRecord does a lot of magic--see http://api.rubyonrails.org/files/activerecord/README_rdoc.html for a basic introduction.)