rails/rails — pull request #6007 by aderyabin (2012-04-27 03:56:41+00:00)
Title: handle includes/join correctly for calculation

This problem very detailed described at this [issue](https://github.com/rails/rails/issues/5990).

Also in progress of work with this issue I find that something like this returns error

```ruby
Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
Company.includes(:contracts).maximum(:developer_id)
```

This bug fixed at second commit in this pull request