ignoring order when paginating a has_many relation
Reported by ronin-5252 (at lighthouseapp) | July 15th, 2007 @ 09:03 PM
has_many :forum_topics, :order => "is_sticky desc, last_post_created_on desc", :dependent => :destroy do
def last
@last_topic ||= find(:first, :order => "last_post_created_on DESC")
end
end
Which works fine with a normal find, but the following ignores the order:
@topics = @forum.forum_topics.paginate :page => params[:page]
and forces me thus to add ':order => "is_sticky desc, last_post_created_on desc"' again, which I don't like at all.
Comments and changes to this ticket
-
Chris Wanstrath July 15th, 2007 @ 09:03 PM
- State changed from new to open
- Assigned user changed from Chris Wanstrath to Mislav
-
Chris Wanstrath July 15th, 2007 @ 09:03 PM
- 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 July 15th, 2007 @ 09:03 PM
- 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 July 15th, 2007 @ 09:03 PM
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 July 15th, 2007 @ 09:03 PM
- 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 July 15th, 2007 @ 09:03 PM
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 end
Thanks for your help!
-
Stet July 15th, 2007 @ 09:03 PM
Erk, sorry, duff copy and paste there. The user model is this:
class User < ActiveRecord::Base has_many :posts, :order=>'created_at DESC'
-
Mislav July 15th, 2007 @ 09:03 PM
When I said "run rake", I meant that you run it in the plugin dir
$ cd vendor/plugins/will_paginate $ rake
-
Stet July 15th, 2007 @ 09:03 PM
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 July 15th, 2007 @ 09:03 PM
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 0
However, 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, 30
This is weird and it's late so I'm going to bed.
-
Mislav July 15th, 2007 @ 09:03 PM
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.
-
Chris Wanstrath July 15th, 2007 @ 09:03 PM
- 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.
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!