Fantastic, thanks Jens.

BTW, I can't :order by :asset_count as it's a method, rather than an
instance variable.  I wish AR::find would let me order by method returns.

While we're talking, i just tried another way of searching and found another
bit of weirdness:  instead of find_with_ferret, i tried using

ActsAsFerret::find(term, class_array) instead, like so:

       #sorting/pagination is broken here
       @results = ActsAsFerret::find(search_term, [TeachingObject],
                      #(ferret) options
                      { :page => options[:page],
                        :per_page => options[:per_page],
                        :sort => Ferret::Search::SortField.new(:asset_count,
:type => :integer, :reverse => true )
                      },
                      #find options - need to specify conditions for each
searched class individually
                      {:conditions => { :teaching_object => ["id in (?)",
@ids] } }
                   )

This gave exactly the same results as the previous search.  However, when i
added another class to the search, it works!

       #this works!
       @results = ActsAsFerret::find(search_term, [TeachingObject,
LearningObject],
                      #(ferret) options
                      { :page => options[:page],
                        :per_page => options[:per_page],
                        :sort => Ferret::Search::SortField.new(:asset_count,
:type => :integer, :reverse => true )
                      },
                      #find options - need to specify conditions for each
searched class individually
                      {:conditions => { :teaching_object => ["id in (?)",
@ids],
                                               :learning_object => ["id in
(?)", @ids] } }
                   )

So, this works while the previous doesn't.  It so happens in this case that
LearningObject is a 'sister' class of TeachingObject (they both extend a
class called Resource), where both are saved in a table called resources
using STI, and at the moment i don't actually have any LearningObject
records, so adding LearningObject doesn't harm my results.  Obviously though
this isn't a nice workaround.

Sorry to pile bug reports on you, i just mention it in case it's relevant.
:/

Thanks a lot
max

2008/4/25 Jens Kraemer <[EMAIL PROTECTED]>:

> Hi Max,
>
> thanks for your detailed report. Might well be that I broke one or more
> of the various combinations of pagination / sorting / active record
> conditions (where you might specify :order, too, btw) in trunk.
>
> I'll look into it asap.
>
> Cheers,
> Jens
>
> On Fri, Apr 25, 2008 at 11:45:30AM +0100, Max Williams wrote:
> > Hi
> >
> > I have a problem with a search where i want to get some results according
> to
> > some conditions, sort the results, and then paginate over the sorted
> > collection.
> >
> > My search looks like this:
> >
> >       @results  = TeachingObject.find_with_ferret(search_term,
> >                         #(ferret) options
> >                         {:page => options[:page],
> >                         :per_page => options[:per_page],
> >                         :sort =>
> Ferret::Search::SortField.new(:asset_count,
> > :type => :integer, :reverse => true )},
> >                         #find options
> >                         { :conditions => ["id in (?)", @ids] } )
> >
> > where @ids is an array of ids from which the results must come (ie a
> > collection of 'allowed' results of which @results will be a subset):
> often
> > the search term is set to * to get all of this collection in @results.
> > ':asset_count' is an untokenized ferret field that stores integers.
> >
> > Through debugging and experimenting, i've observed the following:
> >
> >    - The overall results set, without sorting/pagination, is correct
> >    (therefore :conditions is being taken into account).
> >    - If :per_page is set to be so large that no pagination is required,
> then
> >    the sorting occurs properly (therefore sorting is being taken into
> account)
> >    - If :per_page is reduced so that pagination is required, then the
> >    sorting of the overall set breaks: it seems as if the results are
> ordered by
> >    id, then paginated.
> >    - However, on every individual page, the results are sorted properly
> for
> >    that page, ie each page-size subset is internally sorted.
> >    - If i sort on a different untokenized field,  the problem persists.
> >
> > It seems as if the pagination is happening and THEN the sorting is
> > happening, which obviously doesn't give the expected results.  This is
> just
> > a theory on my part though.
> >
> > Can anyone tell me how to fix this problem?  I've been gnashing my teeth
> > over it for over a day now and can't find any solutions...
> >
> > thanks
> > max
>
> > _______________________________________________
> > Ferret-talk mailing list
> > [email protected]
> > http://rubyforge.org/mailman/listinfo/ferret-talk
>
> --
> Jens Krämer
> webit! Gesellschaft für neue Medien mbH
> Schnorrstraße 76 | 01069 Dresden
> Telefon +49 351 46766-0 | Telefax +49 351 46766-66
> [EMAIL PROTECTED] | www.webit.de
>
> Amtsgericht Dresden | HRB 15422
> GF Sven Haubold
> _______________________________________________
> Ferret-talk mailing list
> [email protected]
> http://rubyforge.org/mailman/listinfo/ferret-talk
>
_______________________________________________
Ferret-talk mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ferret-talk

Reply via email to