order not honored with uniq
Reported by sjh | November 22nd, 2007 @ 10:27 AM
in a controller, I have this code
@movies = @person.movies.uniq.paginate :page => params[:page], :order => params[:sort]
but the :order statement in not honored. It works if I take out uniq
@movies = @person.movies.paginate :page => params[:page], :order => params[:sort]
I'm using instantRails 1.2.3. and I installed the will_paginate plugin yesterday. If you could tell me if there is an easy way to fix this I would be greatful!
Thanks for a great plugin.
Shirl Hart
Comments and changes to this ticket
-
Chris Wanstrath November 22nd, 2007 @ 12:09 PM
- → Assigned user changed from Chris Wanstrath to Mislav
- → State changed from new to open
-
Mislav December 19th, 2007 @ 07:18 AM
- → State changed from open to invalid
Hey Shirl,
This is in fact your fault. You're mixing database operations with local ones (the uniq method on Arrays). This is not a good approach, since you are using pagination which means all logic should be handled on database level.
Also, uniq has no effect on the result of an ActiveRecord find/paginate because every record returned is unique (primary keys, remember?).
You could try something like:
@person.movies.paginate :select => "DISTINCT title, year", :page => params[:page], :order => params[:sort]Other solution could be using GROUP BY in SQL.
You can post to our discussion Google group to explain why did you try `uniq` in the first place and ask for advice how to solve it on the database level.
-

sjh December 23rd, 2007 @ 07:52 AM
adding :select => 'DISTINCT movies.*' worked! Thanks for your help.
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 »
