In Rails 3.1 Model.count() ignores :include - no outer join in generated SQL

Hello!

At first I posted this in rubyonrails-talk, but no one there was able to tell me if this is a bug or not (I got only suggestion for workaround).
As this looks like 3.1 regression that breaks existing 3.0.10 code I decided to post this here in addition.

I’ve upgraded to Rails 3.1 (from 3.0.10), ran my test suite and found this issue:

class Trade < ActiveRecord::Base
has_many :transaction_trades

def Trade.do_something
stale_trades = Trade.count(‘transaction_trades.id’,
:include => :transaction_trades,
:group => ‘trades.id’)
:having => “count_transaction_trades_id = 0”)
end
end

Earlier this used to work perfectly, now I get this:

Mysql2::Error: Unknown column ‘transaction_trades.id’ in ‘field list’:
SELECT COUNT(transaction_trades.id) AS count_transaction_trades_id,
trades.id AS trades_id FROM trades GROUP BY trades.id

What puzzles me is that no LEFT OUTER JOIN on transaction_trades is
present in generated SQL - that’s why things broke.
And it was there in 3.0.10, so table got joined and this code was perfectly working.

Any hints what’s wrong here? Why is :include => :transaction_trades seems to be ignored?

Hi,

Here is pull request for this bug https://github.com/rails/rails/pull/2896.

Thanks,
Anatoliy

Oh, great, thanks for the heads up!

I really know almost nothing about RoR release process, so one more little question:

should I expect this fix to be in the next minor release (i.e. 3.1.1) ?

Thanks :slight_smile:

Dmitry.

We're aiming to release 3.1.1 on Monday 12th, and if the pull request
can be merged before then, it will be.

That would be great! Thank you! :slight_smile: