Combining associations and include fails
Reported by Matijs van Zuijlen | August 27th, 2007 @ 02:13 PM
In will_paginate, if a company has workers, and a worker has a car, something like
company.workers.paginate(:page => 1, :include => :cars, :conditions => "cars.age > 10")
will fail to include the cars table in the count query.
Comments and changes to this ticket
-
Matijs van Zuijlen August 27th, 2007 @ 02:18 PM
Here is a patch with tests revealing the problem. The first test tests that this works in the case of e.g. Workers.paginate, the second (failing) test tests the combination of associations and include.
I'm putting the patch inline since lighthouse won't attach anything right now.
diff --git a/vendor/plugins/will_paginate/test/finder_test.rb b/vendor/plugins/will_paginate/test/finder_test.rb index b69c442..f91edaf 100644 --- a/vendor/plugins/will_paginate/test/finder_test.rb +++ b/vendor/plugins/will_paginate/test/finder_test.rb @@ -125,6 +125,37 @@ class FinderTest < ActiveRecordTestCase assert_equal expected, entries.to_a end + def test_paginate_with_include_and_conditions + entries = Topic.paginate \ + :page => 1, + :include => :replies, + :conditions => "replies.content LIKE 'Bird%' ", + :per_page => 10 + + expected = Topic.find :all, + :include => 'replies', + :conditions => "replies.content LIKE 'Bird%' ", + :limit => 10 + + assert_equal expected, entries.to_a + end + + def test_paginate_associations_with_include + project = projects :active_record + project.topics.paginate \ + :page => 1, + :include => :replies, + :conditions => "replies.content LIKE 'Nice%' ", + :per_page => 10 + + expected = Topic.find :all, + :include => 'replies', + :conditions => "project_id = #{project.id} AND replies.content LIKE 'Nice%' ", + :limit => 10 + + assert_equal expected, entries.to_a + end + def test_paginate_with_group entries = Developer.paginate :page => 1, :per_page => 10, :group => 'salary' expected = [ users(:david), users(:jamis), users(:dev_10), users(:poor_jamis) ].map(&:salary).sort
-
Chris Wanstrath August 27th, 2007 @ 06:04 PM
- Assigned user changed from Chris Wanstrath to Mislav
- State changed from new to open
-
Matijs van Zuijlen August 28th, 2007 @ 12:00 AM
It seems this is due to Rails internals wiping out the :include option. I've fixed ActiveRecord::Associations::HasManyAssociation#count so the test passes[1], but will still need to test if it doesn't mess up something else.
[1] There's a bug in the above tests, after the point where it fails: entries is not assigned to.
-
Chris Wanstrath August 28th, 2007 @ 12:49 AM
- State changed from open to invalid
(from [341]) Will Paginate: more test coverage for combining association pagination with the :include option. The test fails with Rails 1.2.3, but the user is advised to upgrade to trunk or the stable branch. [Matijs van Zuijlen] [#93 state:invalid]
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป
Everyone's favorite Ruby library for pagination of practically anything!