Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

Combining associations and include fails

#93

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.

Reported by Matijs van Zuijlen · August 27th, 2007 @ 02:13 PM

State: invalid
Milestone: none
Assigned to: Mislav Mislav

Activity

  1. Matijs van Zuijlen
    Matijs van Zuijlen

    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
    

    August 27th, 2007 @ 02:18 PM

  2. Chris Wanstrath
    Chris Wanstrath
    • Assigned user changed from Chris Wanstrath to Mislav
    • State changed from new to open

    August 27th, 2007 @ 06:04 PM

  3. Matijs van Zuijlen
    Matijs van Zuijlen

    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.

    August 28th, 2007 @ 12:00 AM

  4. Chris Wanstrath
    Chris Wanstrath
    • 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]

    http://plugins.require.errtheblo...

    August 28th, 2007 @ 12:49 AM

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.