Find_by_contents + 'conditions' returning incorrect results

I’ve read the other threads like this one
(http://www.ruby-forum.com/topic/78841) but I’m not sure what I’m doing
wrong.

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
hits):

Lender.find_by_contents(“bank”)

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
SQL:

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?

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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs