title: Grouping rails records by week and adding them
body: <p>I'm using Rails 4.1 and Ruby 2.1 and Postgresql</p>
  
  <p>I have a records table with a day column (datetime), and a value column (decimal). I want to add all the value columns that are in the same week accounting for the time zone of the user available here: current_user.time_zone</p>
  
  <p>The records could be in the 1000s so speed is important.</p>
  
  <p>I used the groupdate gem like this:</p>
  
  <pre><code>Record.where(:user_id =&gt; current_user.id).group_by_week(:day, time_zone: current_user.time_zone).count
  </code></pre>
  
  <p>But it returned a hash of 4 datetimes, and there are 9 records in total.</p>
  
  <p><strong>Solution from BroiSatse:</strong> </p>
  
  <p>Record.where(:user_id => current_user.id).group_by_week(:day, time_zone: current_user.time_zone).sum(:value) </p>
  
  <p><strong>However when :value is json This is the error I get:</strong></p>
  
  <pre><code>SELECT SUM(value-&gt;'430') AS sum_value_430, (DATE_TRUNC('week', (day::timestamptz - INTERVAL '6 day' - INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai') + INTERVAL '6 day' + INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai' AS week FROM "eats"  WHERE "eats"."user_id" = 53 AND (day IS NOT NULL) GROUP BY (DATE_TRUNC('week', (day::timestamptz - INTERVAL '6 day' - INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai') + INTERVAL '6 day' + INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai'
  PG::UndefinedFunction: ERROR:  function sum(json) does not exist
  LINE 1: SELECT SUM(value-&gt;'430') AS sum_total_nutriti...
                 ^
  HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
  : SELECT SUM(value-&gt;'430') AS sum_value_430, (DATE_TRUNC('week', (day::timestamptz - INTERVAL '6 day' - INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai') + INTERVAL '6 day' + INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai' AS week FROM "eats"  WHERE "eats"."user_id" = 53 AND (day IS NOT NULL) GROUP BY (DATE_TRUNC('week', (day::timestamptz - INTERVAL '6 day' - INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai') + INTERVAL '6 day' + INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai'
  ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR:  function sum(json) does not exist
  LINE 1: SELECT SUM(value-&gt;'430') AS sum_total_nutriti...
                 ^
  HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
  : SELECT SUM(value-&gt;'430') AS sum_value_430, (DATE_TRUNC('week', (day::timestamptz - INTERVAL '6 day' - INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai') + INTERVAL '6 day' + INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai' AS week FROM "eats"  WHERE "eats"."user_id" = 53 AND (day IS NOT NULL) GROUP BY (DATE_TRUNC('week', (day::timestamptz - INTERVAL '6 day' - INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai') + INTERVAL '6 day' + INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai'
  </code></pre>