title: Rails 4 Sum by Model Method
body: <p>In my app, I have a <code>User</code> model, with a <code>goal_ytd</code> method, which performs some calculations.</p>
  
  <p>In a controller, I have a variable <code>@users</code> that might be <code>User</code> or an <code>ActiveRecord::Relation</code> of <code>users</code>, and I would like to sum all of the <code>@users</code>'s <code>goal_ytd</code>s.</p>
  
  <p>My first inclination was:</p>
  
  <pre><code>@users.sum(&amp;:goal_ytd)
  </code></pre>
  
  <p>Which threw a deprecation warning in both cases because using <code>sum</code> on an <code>ActiveRecord::Relation</code> is going away in Rails 4.1.</p>
  
  <p>So, I changed the code to:</p>
  
  <pre><code>@users.to_a.sum(&amp;:goal_ytd)
  </code></pre>
  
  <p>Which then threw a <code>NoMethodError</code> because, in a certain circumstance, <code>@users</code> is assigned by <code>@users = User</code> and <code>User</code> has no <code>to_a</code> method.</p>
  
  <p>Assigning <code>@users</code> using <code>@users = User.all</code> throws a deprecation warning because <code>Relation#all</code> is also deprecated.</p>
  
  <p>Is there a way to get all <code>Users</code> as an array? Is there a better way?</p>