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.

order not honored with uniq

#161

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

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

State: resolved
Milestone: none
Assigned to: Mislav Mislav

Activity

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

    November 22nd, 2007 @ 09:09 PM

  2. Mislav
    Mislav
    • 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.

    December 19th, 2007 @ 04:18 PM

  3. sjh
    sjh

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

    December 23rd, 2007 @ 04:52 PM

  4. Mislav
    Mislav
    • 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
    https://github.com/mislav/will_paginate/commit/8ce22768c1f9f19c86e5...

    September 9th, 2011 @ 12:52 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.