Rails 4 AR .select includes pk field in result even if result is grouped (as opposed to Rails 3.2)

21 views Asked by At

I'm migrating my app from Rails 3.2 to 4.0, and I'm finding a number of broken tests because Rails 4 includes the PK field (usually 'id') in the result set when using something like:

Invoice.where(cust_id: 1234).select('sum(gross) as gross, sum(tax) as tax, sum(net) as net').to_a.first.attributes

This used to return {'gross' => 1000.00, 'tax' => 0.00, 'net' => 1000.00} in 3.2, but in 4.0 it returns {'gross' => 1000.00, 'tax' => 0.00, 'net' => 1000.00, id => nil}

This is a problem if you're writing tests and comparing actual results to expected results, in which case the id field throws off comparisons. I don't see this change in behavior documented anywhere. Is there a way to turn this "feature" off?

0

There are 0 answers