Forum: Ferret find_by_contents + 'conditions' returning incorrect results

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
27a88692ed54ebdded2174062f4ebc0c?d=identicon&s=25 Marc Garrett (since1968)
on 2007-05-18 21:45
I've read the other threads like this one
( but I'm not sure what I'm doing

Scenario: I need a full text search on financial institutions and to
constrain that list by state (=) or states (IN). My lenders model
includes a name and a state, among other fields. In my lenders model:

acts_as_ferret :fields => [:name]

Running this in script/console, I get a correct result (about 2100


But when I start constraining with active record I get weird results.
This turns up one hit, which is correct:

Lender.find_by_contents("suntrust", {}, {:conditions=>["sta1 = 'DC'"]})

But this turns up zero hits, when it should be 11 hits according to my

Lender.find_by_contents("bank", {}, {:conditions=>["sta1 = 'DC'"]})

Also, an IN search doesn't appear to constrain results at all. This
should turn up 16 rows but it turns up 31 (all the suntrusts, not
constrained by state):

Lender.find_by_contents("suntrust", {:conditions=>["sta1 IN ?",
"('AL','DC', 'FL')"]})

Any ideas?
27a88692ed54ebdded2174062f4ebc0c?d=identicon&s=25 Marc Garrett (since1968)
on 2007-05-19 00:35
OK, solved my own problem--the answer was implicit in a lot of Jens'
responses here but I didn't grok it. Here's the correct code:

Lender.find_by_contents(params[:name], {:limit=>:all},
{:conditions=>["sta1 = 'DC'"]})

Reason: If you don't use the :limit option, find_by_contents limits the
number of results automatically, and does so BEFORE ActiveRecord applies
the conditions.

Hope this helps someone.
This topic is locked and can not be replied to.