Fantastic, thanks Jens.<br><br>BTW, I can&#39;t :order by :asset_count as it&#39;s a method, rather than an instance variable.&nbsp; I wish AR::find would let me order by method returns.<br><br>While we&#39;re talking, i just tried another way of searching and found another bit of weirdness:&nbsp; instead of find_with_ferret, i tried using <br>
<br>ActsAsFerret::find(term, class_array) instead, like so:<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #sorting/pagination is broken here<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @results = ActsAsFerret::find(search_term, [TeachingObject],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #(ferret) options <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { :page =&gt; options[:page],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :per_page =&gt; options[:per_page],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :sort =&gt; Ferret::Search::SortField.new(:asset_count, :type =&gt; :integer, :reverse =&gt; true )<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; },<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #find options - need to specify conditions for each searched class individually<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {:conditions =&gt; { :teaching_object =&gt; [&quot;id in (?)&quot;, @ids] } }<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )&nbsp;&nbsp;&nbsp; <br><br>This gave exactly the same results as the previous search.&nbsp; However, when i added another class to the search, it works!&nbsp; <br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #this works!<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @results = ActsAsFerret::find(search_term, [TeachingObject, LearningObject],<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #(ferret) options <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { :page =&gt; options[:page],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :per_page =&gt; options[:per_page],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :sort =&gt; Ferret::Search::SortField.new(:asset_count, :type =&gt; :integer, :reverse =&gt; true )<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; },<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #find options - need to specify conditions for each searched class individually<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {:conditions =&gt; { :teaching_object =&gt; [&quot;id in (?)&quot;, @ids],<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :learning_object =&gt; [&quot;id in (?)&quot;, @ids] } }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )&nbsp; <br><br>So, this works while the previous doesn&#39;t.&nbsp; It so happens in this case that LearningObject is a &#39;sister&#39; 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&#39;t actually have any LearningObject records, so adding LearningObject doesn&#39;t harm my results.&nbsp; Obviously though this isn&#39;t a nice workaround.<br>
<br>Sorry to pile bug reports on you, i just mention it in case it&#39;s relevant.&nbsp; :/<br><br>Thanks a lot<br>max<br><br><div class="gmail_quote">2008/4/25 Jens Kraemer &lt;<a href="mailto:kraemer@webit.de">kraemer@webit.de</a>&gt;:<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&#39;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>
&gt; Hi<br>
&gt;<br>
&gt; I have a problem with a search where i want to get some results according to<br>
&gt; some conditions, sort the results, and then paginate over the sorted<br>
&gt; collection.<br>
&gt;<br>
&gt; My search looks like this:<br>
&gt;<br>
&gt; &nbsp; &nbsp; &nbsp; @results &nbsp;= TeachingObject.find_with_ferret(search_term,<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #(ferret) options<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {:page =&gt; options[:page],<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :per_page =&gt; options[:per_page],<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :sort =&gt; Ferret::Search::SortField.new(:asset_count,<br>
&gt; :type =&gt; :integer, :reverse =&gt; true )},<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #find options<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { :conditions =&gt; [&quot;id in (?)&quot;, @ids] } )<br>
&gt;<br>
&gt; where @ids is an array of ids from which the results must come (ie a<br>
&gt; collection of &#39;allowed&#39; results of which @results will be a subset): often<br>
&gt; the search term is set to * to get all of this collection in @results.<br>
&gt; &#39;:asset_count&#39; is an untokenized ferret field that stores integers.<br>
&gt;<br>
&gt; Through debugging and experimenting, i&#39;ve observed the following:<br>
&gt;<br>
&gt; &nbsp; &nbsp;- The overall results set, without sorting/pagination, is correct<br>
&gt; &nbsp; &nbsp;(therefore :conditions is being taken into account).<br>
&gt; &nbsp; &nbsp;- If :per_page is set to be so large that no pagination is required, then<br>
&gt; &nbsp; &nbsp;the sorting occurs properly (therefore sorting is being taken into account)<br>
&gt; &nbsp; &nbsp;- If :per_page is reduced so that pagination is required, then the<br>
&gt; &nbsp; &nbsp;sorting of the overall set breaks: it seems as if the results are ordered by<br>
&gt; &nbsp; &nbsp;id, then paginated.<br>
&gt; &nbsp; &nbsp;- However, on every individual page, the results are sorted properly for<br>
&gt; &nbsp; &nbsp;that page, ie each page-size subset is internally sorted.<br>
&gt; &nbsp; &nbsp;- If i sort on a different untokenized field, &nbsp;the problem persists.<br>
&gt;<br>
&gt; It seems as if the pagination is happening and THEN the sorting is<br>
&gt; happening, which obviously doesn&#39;t give the expected results. &nbsp;This is just<br>
&gt; a theory on my part though.<br>
&gt;<br>
&gt; Can anyone tell me how to fix this problem? &nbsp;I&#39;ve been gnashing my teeth<br>
&gt; over it for over a day now and can&#39;t find any solutions...<br>
&gt;<br>
&gt; thanks<br>
&gt; max<br>
<br>
</div></div>&gt; _______________________________________________<br>
&gt; Ferret-talk mailing list<br>
&gt; <a href="mailto:Ferret-talk@rubyforge.org">Ferret-talk@rubyforge.org</a><br>
&gt; <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>