#161 ✓resolved

order not honored with uniq

Reported by sjh | November 22nd, 2007 @ 07:27 PM

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

    Chris Wanstrath November 22nd, 2007 @ 09:09 PM

    • Assigned user changed from “Chris Wanstrath” to “Mislav”
    • State changed from “new” to “open”
  • Mislav

    Mislav December 19th, 2007 @ 04:18 PM

    • 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

    sjh December 23rd, 2007 @ 04:52 PM

    adding :select => 'DISTINCT movies.*' worked! Thanks for your help.

  • Mislav

    Mislav September 9th, 2011 @ 12:52 AM

    • Tag set to will_paginate
    • State changed from “invalid” to “resolved”
    • Milestone order changed from “0” to “0”

    (from [8ce22768c1f9f19c86e50babe1eeac5f5f9741b0]) Active Record: fix empty? on a relation with a group clause

    Fixes #161, fixes #167

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.

New-ticket Create new ticket

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!

People watching this ticket