size method ignores "group by" on has_many
Reported by matthewgarysmith (at gmail) | August 29th, 2008 @ 09:26 PM | in 2.x
A has_many that uses the :group option, returns invalid results for the size method.
class Book < ActiveRecord::Base
has_many :reviews
has_many :reviewers, :through => :reviews, :group => "reviews.id"
end
class Review < ActiveRecord::Base
belongs_to :book
belongs_to :reviewer
end
class Reviewer < ActiveRecord::Base
has_many :reviews
has_many :books, :through => :reviews, :group => "books.id"
end
After create a bunch of reviews...
alan = Reviewer.find_by_name("alan")
alan.books.size # => 4
alan.books.to_a.size # => 2
This is the SQL generated by the second line ignores the :group option:
SELECT count(*) AS count_all FROM "books" INNER JOIN reviews ON books.id = reviews.book_id WHERE (("reviews".reviewer_id = 1))
it should be more like the follow, no?
SELECT count(distinct books.id) AS count_all FROM "books" INNER JOIN reviews ON books.id = reviews.book_id WHERE (("reviews".reviewer_id = 1))
Attached are detailed steps to reproduce the bug.
Comments and changes to this ticket
-
Assaf Arkin September 10th, 2008 @ 08:42 PM
I'm pretty sure this used to work before, 2.1.0 and edge.
-
miloops September 12th, 2008 @ 04:12 PM
- → Tag changed from activerecord bug has_many to activerecord bug has_many patch
Here is the patch to fix this, includes a test to verify that the :group option is included when the target hasn't been loaded.
-
miloops September 15th, 2008 @ 05:25 PM
- no changes were found...
-

Repository September 15th, 2008 @ 05:33 PM
- → State changed from new to committed
(from [dc8bf7515de85f5bc28d17e96edf4a3e74a858da]) When counting grouped records the target should be loaded to return a valid groups count result.
Without this change count_records will group for the count in the query and return erroneous results.
Signed-off-by: Michael Koziarski michael@koziarski.com [#937 state:committed] http://github.com/rails/rails/co...
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
Source available from github
Repository is at http://github.com/rails/rails
Check out the development master (Edge Rails):
git clone git://github.com/rails/rails.git
Creating or reviewing a patch
See the contributor guide.
Creating a feature request
Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.
Creating a bug report
When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.
Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.
Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too"..
