Forum: Ferret Wrong total_hits when using conditions in find_by_contents

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Carsten G. (Guest)
on 2007-01-16 01:47
I don't know if this is a bug, or wanted behavior, but for me it was a
pain in... So here's the problem + a bugfix.

Lets say you have a model "Article" with the following fields: title,
visible - and these records

[code]title, visible
ferret talk, 1
ruby talk, 0
ruby on rails, 1
lets talk about ruby, 1[/code]

If I let Article act as a ferret, and do:

result = Article.find_by_content('ruby')

Result will contain 3 items and "total_hits" will return 3

However, if I add a condition:

result = Article.find_by_content('ruby', {}, 'visible = 1')

Result will contain 2 items - which is correct

But "hotal_hits" will still return 3 - not what I would expect.

-----------------------------
Fix for this:

1) In the acts_as_ferret plugin, find the file class_methods.rb

2) Go to line 276 where you have this code-block

[code]if results.any?
   conditions = combine_conditions([ "#{table_name}.#{primary_key} in
(?)", results.keys ],
   find_options[:conditions])
   result = self.find(:all,
   find_options.merge(:conditions => conditions))
end[/code]

and add this line:

[code]if results.any?
   conditions = combine_conditions([ "#{table_name}.#{primary_key} in
(?)", results.keys ],
   find_options[:conditions])
   result = self.find(:all,
   find_options.merge(:conditions => conditions))
   total_hits = result.length   <===== ADD THIS!!!
end[/code]

- Carsten
Carsten G. (Guest)
on 2007-01-16 01:48
Ah bloody hell...

Sorry for the strange tags, I thought the forum supported BBCode...

- Carsten
Jens K. (Guest)
on 2007-01-16 13:13
(Received via mailing list)
On Tue, Jan 16, 2007 at 12:48:07AM +0100, Carsten G. wrote:
> Ah bloody hell...
>
> Sorry for the strange tags, I thought the forum supported BBCode...

that's because it's a mailing list in the first place ;-)

Jens



--
webit! Gesellschaft für neue Medien mbH          www.webit.de
Dipl.-Wirtschaftsingenieur Jens Krämer 
removed_email_address@domain.invalid
Schnorrstraße 76                         Tel +49 351 46766  0
D-01069 Dresden                          Fax +49 351 46766 66
Jens K. (Guest)
on 2007-01-16 13:13
(Received via mailing list)
Hi!

On Tue, Jan 16, 2007 at 12:47:17AM +0100, Carsten G. wrote:
> I don't know if this is a bug, or wanted behavior, but for me it was a
> pain in... So here's the problem + a bugfix.

right, that's a bug. I just committed a less invasive fix that will only
set total_hits to the Active Record result set size if the user gave any
active record conditions with his queries (see below).

But please keep in mind that total_hits still may be wrong under certain
circumstances - e.g. if you specify the :num_docs ferret option and some
active record conditions further limiting the result set.

>
> result = Article.find_by_content('ruby')
>
> Result will contain 3 items and "total_hits" will return 3
>
> However, if I add a condition:
> result = Article.find_by_content('ruby', {}, 'visible = 1')
> Result will contain 2 items - which is correct
>
> But "hotal_hits" will still return 3 - not what I would expect.

>
> if results.any?
>    conditions = combine_conditions([ "#{table_name}.#{primary_key} in
> (?)", results.keys ],
>    find_options[:conditions])
>    result = self.find(:all,
>    find_options.merge(:conditions => conditions))
>    total_hits = result.length   <===== ADD THIS!!!

even better, add

    total_hits = result.length if find_options[:conditions]

so total_hits stays correct if you use any ferret options like :num_docs
instead of AR conditions to limit the result set.

cheers,
Jens

--
webit! Gesellschaft für neue Medien mbH          www.webit.de
Dipl.-Wirtschaftsingenieur Jens Krämer 
removed_email_address@domain.invalid
Schnorrstraße 76                         Tel +49 351 46766  0
D-01069 Dresden                          Fax +49 351 46766 66
This topic is locked and can not be replied to.