# Find\_by\_contents + 'conditions' returning incorrect results

**URL:** <https://www.ruby-forum.com/t/find-by-contents-conditions-returning-incorrect-results/104514>\
**Category:** Ferret\
**Created:** [May 18, 2007, 9:45pm UTC](https://www.ruby-forum.com/t/find-by-contents-conditions-returning-incorrect-results/104514 "2007-05-18T21:45:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![since1968](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9a140/32.png) [@since1968](https://www.ruby-forum.com/u/since1968)\
**Post date:** [May 18, 2007, 9:45pm UTC](https://www.ruby-forum.com/t/find-by-contents-conditions-returning-incorrect-results/104514/1 "2007-05-18T21:45:54Z")

</div>

I’ve read the other threads like this one  
([Using conditions - Ferret - Ruby-Forum](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?

---

<div class="post-metadata">

**Author:** ![since1968](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9a140/32.png) [@since1968](https://www.ruby-forum.com/u/since1968)\
**Post date:** [May 19, 2007, 12:35am UTC](https://www.ruby-forum.com/t/find-by-contents-conditions-returning-incorrect-results/104514/2 "2007-05-19T00:35:45Z")

</div>

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.
