Fantastic, thanks Jens.<br><br>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.<br><br>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 <br>
<br>ActsAsFerret::find(term, class_array) instead, like so:<br><br> #sorting/pagination is broken here<br> @results = ActsAsFerret::find(search_term, [TeachingObject],<br> #(ferret) options <br>
{ :page => options[:page],<br> :per_page => options[:per_page],<br> :sort => Ferret::Search::SortField.new(:asset_count, :type => :integer, :reverse => true )<br>
},<br> #find options - need to specify conditions for each searched class individually<br> {:conditions => { :teaching_object => ["id in (?)", @ids] } }<br>
) <br><br>This gave exactly the same results as the previous search. However, when i added another class to the search, it works! <br><br> #this works!<br> @results = ActsAsFerret::find(search_term, [TeachingObject, LearningObject],<br>
#(ferret) options <br> { :page => options[:page],<br> :per_page => options[:per_page],<br> :sort => Ferret::Search::SortField.new(:asset_count, :type => :integer, :reverse => true )<br>
},<br> #find options - need to specify conditions for each searched class individually<br> {:conditions => { :teaching_object => ["id in (?)", @ids],<br>
:learning_object => ["id in (?)", @ids] } }<br> ) <br><br>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.<br>
<br>Sorry to pile bug reports on you, i just mention it in case it's relevant. :/<br><br>Thanks a lot<br>max<br><br><div class="gmail_quote">2008/4/25 Jens Kraemer <<a href="mailto:kraemer@webit.de">kraemer@webit.de</a>>:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Hi Max,<br>
<br>
thanks for your detailed report. Might well be that I broke one or more<br>
of the various combinations of pagination / sorting / active record<br>
conditions (where you might specify :order, too, btw) in trunk.<br>
<br>
I'll look into it asap.<br>
<br>
Cheers,<br>
Jens<br>
<div><div></div><div class="Wj3C7c"><br>
On Fri, Apr 25, 2008 at 11:45:30AM +0100, Max Williams wrote:<br>
> Hi<br>
><br>
> I have a problem with a search where i want to get some results according to<br>
> some conditions, sort the results, and then paginate over the sorted<br>
> collection.<br>
><br>
> My search looks like this:<br>
><br>
> @results = TeachingObject.find_with_ferret(search_term,<br>
> #(ferret) options<br>
> {:page => options[:page],<br>
> :per_page => options[:per_page],<br>
> :sort => Ferret::Search::SortField.new(:asset_count,<br>
> :type => :integer, :reverse => true )},<br>
> #find options<br>
> { :conditions => ["id in (?)", @ids] } )<br>
><br>
> where @ids is an array of ids from which the results must come (ie a<br>
> collection of 'allowed' results of which @results will be a subset): often<br>
> the search term is set to * to get all of this collection in @results.<br>
> ':asset_count' is an untokenized ferret field that stores integers.<br>
><br>
> Through debugging and experimenting, i've observed the following:<br>
><br>
> - The overall results set, without sorting/pagination, is correct<br>
> (therefore :conditions is being taken into account).<br>
> - If :per_page is set to be so large that no pagination is required, then<br>
> the sorting occurs properly (therefore sorting is being taken into account)<br>
> - If :per_page is reduced so that pagination is required, then the<br>
> sorting of the overall set breaks: it seems as if the results are ordered by<br>
> id, then paginated.<br>
> - However, on every individual page, the results are sorted properly for<br>
> that page, ie each page-size subset is internally sorted.<br>
> - If i sort on a different untokenized field, the problem persists.<br>
><br>
> It seems as if the pagination is happening and THEN the sorting is<br>
> happening, which obviously doesn't give the expected results. This is just<br>
> a theory on my part though.<br>
><br>
> Can anyone tell me how to fix this problem? I've been gnashing my teeth<br>
> over it for over a day now and can't find any solutions...<br>
><br>
> thanks<br>
> max<br>
<br>
</div></div>> _______________________________________________<br>
> Ferret-talk mailing list<br>
> <a href="mailto:Ferret-talk@rubyforge.org">Ferret-talk@rubyforge.org</a><br>
> <a href="http://rubyforge.org/mailman/listinfo/ferret-talk" target="_blank">http://rubyforge.org/mailman/listinfo/ferret-talk</a><br>
<br>
--<br>
Jens Krämer<br>
webit! Gesellschaft für neue Medien mbH<br>
Schnorrstraße 76 | 01069 Dresden<br>
Telefon +49 351 46766-0 | Telefax +49 351 46766-66<br>
<a href="mailto:kraemer@webit.de">kraemer@webit.de</a> | <a href="http://www.webit.de" target="_blank">www.webit.de</a><br>
<br>
Amtsgericht Dresden | HRB 15422<br>
GF Sven Haubold<br>
_______________________________________________<br>
Ferret-talk mailing list<br>
<a href="mailto:Ferret-talk@rubyforge.org">Ferret-talk@rubyforge.org</a><br>
<a href="http://rubyforge.org/mailman/listinfo/ferret-talk" target="_blank">http://rubyforge.org/mailman/listinfo/ferret-talk</a><br>
</blockquote></div><br>