ignoring order when paginating a has_many relation
-
Chris Wanstrath
- State changed from new to open
- Assigned user changed from Chris Wanstrath to Mislav
-
Chris Wanstrath
- State changed from open to invalid
(from [281]) Will Paginate: additional tests for habtm and has_many associations with options like :dependent or :order. [#54 state:invalid]
-
Mislav
- Title changed from [will_paginate]ignoring order when paginating a has_many relation to ignoring order when paginating a has_many relation
I added additional tests for this, but they all passed. Did you have will_paginate on earlier revision than [278]? Please update to the latest revision and test for yourself. If it still fails for you like you've described, reopen this ticket.
-
Stet
I'm using revision 286, and see exactly this problem.
Model has:
has_many :posts, :order=>'created_at DESC'but
@posts = current_user.posts.paginate :page=> params[:page]ignores the ordering. current_user.posts orders them as required.
-
Mislav
- State changed from invalid to open
Can you please:
- double-check that there is no ORDER clause in generated SQL (look at the development log)
- tell me what database are you using
- run "rake" from the plugin directory and report the results
- paste the code of User and Post models
-
Stet
1. Yep, there's no ORDER clause:
[0mSELECT * FROM posts LIMIT 0, 10[0m
2. MySQL 5.0.27-standard
3. The tests all pass, 1 test 1 assertion 0 failures 0 errors for post_test.rb and post_controller_test.rb
4. The user model is the standard acts_as_authenticated model. The only addition is
class User < ActiveRecord::Base has_many :notes, :order=>'created_at DESC'The post model is:
class Post < ActiveRecord::Base belongs_to :user validates_presence_of :body, :message=>"of the post has to say something!" def self.per_page 10 end endThanks for your help!
-
Stet
Erk, sorry, duff copy and paste there. The user model is this:
class User < ActiveRecord::Base has_many :posts, :order=>'created_at DESC' -
Mislav
When I said "run rake", I meant that you run it in the plugin dir
$ cd vendor/plugins/will_paginate $ rake -
Stet
Sorry, I'd done cd vendor/plugins. The tests give one failure:
1) Failure: test_paginate_associations(FinderTest) [./test/finder_test.rb:75:in `test_paginate_associations' ./test/helper.rb:26:in `run']: <[#<Project:0x24b1318 @attributes={"name"=>"Active Controller", "id"=>"2"}>, #<Project:0x24b0990 @attributes={"name"=>"Active Record", "id"=>"1"}>]> expected but was <[#<Project:0x24aaf7c @attributes= {"name"=>"Active Record", "project_id"=>"1", "joined_on"=>"2004-10-10", "id"=>"1", "developer_id"=>"1", "access_level"=>"1"}>, #<Project:0x24a9d20 @attributes= {"name"=>"Active Controller", "project_id"=>"2", "joined_on"=>"2004-10-10", "id"=>"2", "developer_id"=>"1", "access_level"=>"1"}>]>. -
Chris Wanstrath
I can confirm this test breaks for me, as well. ORDER is not being added to the SQL query.
>> User.find(:first).projects.paginate(:page => 1) User Load (0.000758) SELECT * FROM users LIMIT 1 SQL (0.000507) SELECT count(*) AS count_all FROM projects INNER JOIN developers_projects ON projects.id = developers_projects.project_id WHERE (developers_projects.developer_id = 1 ) Project Load (0.000747) SELECT * FROM projects INNER JOIN developers_projects ON projects.id = developers_projects.project_id WHERE (developers_projects.developer_id = 1 ) LIMIT 30 OFFSET 0However, in my app it does work:
>> Snippet.find(:first).rewrites.paginate(:page => 1) Snippet Load (0.000651) SELECT * FROM codes WHERE ( (codes.`type` = 'Snippet' ) ) LIMIT 1 SQL (0.000620) SELECT count(*) AS count_all FROM codes WHERE (codes.snippet_id = 1) AND ( (codes.`type` = 'Rewrite' ) ) Rewrite Load (0.000783) SELECT * FROM codes WHERE (codes.snippet_id = 1) AND ( (codes.`type` = 'Rewrite' ) ) ORDER BY votes_count DESC LIMIT 0, 30This is weird and it's late so I'm going to bed.
-
Mislav
Chris: sorry for not posting this earlier, I found it out 2 days ago but couldn't fix it ... Paginating finds on associations (user.projects.paginate) are currently broken if you are using stable Rails. They work on edge.
I will need to backport some AR fixes in edge to the plugin in order to make this work reliably.
-
Mislav
Testing ... something is funny with responding to this ticket
-
Chris Wanstrath
- State changed from open to resolved
(from [298]) Will Paginate: association finders are finally tackled. Will Paginate now works properly on stable Rails when it comes to association pagination like "user.projects.paginate(:page => 1)". [#54 state:resolved]
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.