# Acts\_as\_ferret with conditions

**URL:** <https://www.ruby-forum.com/t/acts-as-ferret-with-conditions/66435>\
**Category:** Rails\
**Created:** [August 25, 2006, 3:22pm UTC](https://www.ruby-forum.com/t/acts-as-ferret-with-conditions/66435 "2006-08-25T15:22:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![welly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f0a364/32.png) [@welly](https://www.ruby-forum.com/u/welly)\
**Post date:** [August 25, 2006, 3:22pm UTC](https://www.ruby-forum.com/t/acts-as-ferret-with-conditions/66435/1 "2006-08-25T15:22:54Z")

</div>

Hello guys,

Having a slight problem with acts\_as\_ferret, specifically using a  
condition. I have the following code

@results = SupplierProduct.find\_by\_contents(params  
[:search], :conditions =\> [‘area\_id = ?’, @area])

and it seems the condition isn’t being applied. Does anyone have any  
pointers?

Cheers,

Alastair

* * *

Alastair M.  
Standards compliant web development with Ruby On Rails, PHP and ASP  
[www.kozmo.co.uk](http://www.kozmo.co.uk)  
07738 399038

---

<div class="post-metadata">

**Author:** ![welly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f0a364/32.png) [@welly](https://www.ruby-forum.com/u/welly)\
**Post date:** [August 25, 2006, 5:29pm UTC](https://www.ruby-forum.com/t/acts-as-ferret-with-conditions/66435/2 "2006-08-25T17:29:15Z")

</div>

I’ve had a look through the AAF source and can’t see why this isn’t  
working. It seems to accept a condition and apply it to the  
activerecord find but it’s simply not happening. Even if I set a  
condition that would normally cause an error, the error isn’t occuring  
so I’m a little stumped here. Would really appreciate any suggestions!

Alastair

---

<div class="post-metadata">

**Author:** ![welly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f0a364/32.png) [@welly](https://www.ruby-forum.com/u/welly)\
**Post date:** [August 25, 2006, 5:35pm UTC](https://www.ruby-forum.com/t/acts-as-ferret-with-conditions/66435/3 "2006-08-25T17:35:16Z")

</div>

Alastair wrote:

> I’ve had a look through the AAF source and can’t see why this isn’t  
> working. It seems to accept a condition and apply it to the  
> activerecord find but it’s simply not happening. Even if I set a  
> condition that would normally cause an error, the error isn’t occuring  
> so I’m a little stumped here. Would really appreciate any suggestions!

Sorry, missed the code I’m working on -

@results = SupplierProduct.find\_by\_contents(params[:search],  
:conditions =\> [‘area\_id = ?’, @area])

---

<div class="post-metadata">

**Author:** ![welly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f0a364/32.png) [@welly](https://www.ruby-forum.com/u/welly)\
**Post date:** [August 26, 2006, 10:51am UTC](https://www.ruby-forum.com/t/acts-as-ferret-with-conditions/66435/4 "2006-08-26T10:51:48Z")

</div>

Alastair,

Looking at the code for find\_by\_contents:

find\_by\_contents(q, options = {}, find\_options = {})

The parameters are the query itself (string or Ferret query), options  
(hash)  
for ferret and find\_options (hash) for AR. What you are passing is  
string  
and a single hash that is going to the ferret options instead of AR. Try  
this:

@results = SupplierProduct.find\_by\_contents(params[:search], {},  
{:conditions =\> [‘area\_id = ?’, @area]})

Besides, the find\_options is to include other related objects (eg.  
{:include  
=\> :suplier\_category}) and not to filter the results. You need to handle  
filtering of results through the ferret query or process the results  
yourself.

Hope it helps,

Adrian M.

On 8/25/06, Alastair M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> >

–  
Adrian Esteban Madrid

---

<div class="post-metadata">

**Author:** ![welly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f0a364/32.png) [@welly](https://www.ruby-forum.com/u/welly)\
**Post date:** [September 4, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/acts-as-ferret-with-conditions/66435/5 "2006-09-04T14:46:24Z")

</div>

Alastair,

The problem with your approach is that it doesn’t work with how  
acts\_as\_ferret (AAF) works. What AAF does is first gets ids of records  
matching your query and then goes one by one doing ModelXyz.find n (n  
being  
the id) with the find options you sent grouping all those results into  
an  
array and returning it. So, you can try using your conditions but it  
might  
break or return an array with some empty slots. In my case I had some  
requirements that made me do my own find\_by\_contents (I named it  
ferret\_find) based on find\_by\_contents. That way I learned exactly how  
it  
works and adapted it for my needs.

Hope it helps,

Adrian M.

On 8/25/06, Alastair M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The parameters are the query itself (string or Ferret query), options  
> yourself.  
> condition and then a free text search for anything more specific. Is this  
> 07738 399038
> 
> >

–  
Adrian Esteban Madrid

---

<div class="post-metadata">

**Author:** ![welly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f0a364/32.png) [@welly](https://www.ruby-forum.com/u/welly)\
**Post date:** [August 26, 2006, 11:58am UTC](https://www.ruby-forum.com/t/acts-as-ferret-with-conditions/66435/6 "2006-08-26T11:58:53Z")

</div>

On 25 Aug 2006, at 19:17, Adrian M. wrote:

> @results = SupplierProduct.find\_by\_contents(params[:search], {},  
> {:conditions =\> [‘area\_id = ?’, @area]})
> 
> Besides, the find\_options is to include other related objects (eg.  
> {:include =\> :suplier\_category}) and not to filter the results. You  
> need to handle filtering of results through the ferret query or  
> process the results yourself.
> 
> Hope it helps,

Hi Adrian,

Many thanks for the help. I was thinking it was a reasonable thing to  
do of using the activerecord “condition” to further filter a search.  
Is there a reason I shouldn’t use this approach? Essentially I’ve got  
a couple of drop down menus (for area and category), which I was  
going to set in the condition and then a free text search for  
anything more specific. Is this not the best way of going about it?

Thanks again,

Alastair

* * *

Alastair M.  
Standards compliant web development with Ruby On Rails, PHP and ASP  
[www.kozmo.co.uk](http://www.kozmo.co.uk)  
07738 399038

---

<div class="post-metadata">

**Author:** ![welly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f0a364/32.png) [@welly](https://www.ruby-forum.com/u/welly)\
**Post date:** [September 5, 2006, 5:51pm UTC](https://www.ruby-forum.com/t/acts-as-ferret-with-conditions/66435/7 "2006-09-05T17:51:02Z")

</div>

Alastair,

Glad to help.

Sincerely,

Adrian M.

On 8/25/06, Alastair M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> being the id) with the find options you sent grouping all those results into  
> Hi Adrian,  
> Standards compliant web development with Ruby On Rails, PHP and ASP  
> [www.kozmo.co.uk](http://www.kozmo.co.uk)  
> 07738 399038
> 
> >

–  
Adrian Esteban Madrid

---

<div class="post-metadata">

**Author:** ![welly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f0a364/32.png) [@welly](https://www.ruby-forum.com/u/welly)\
**Post date:** [September 5, 2006, 5:50pm UTC](https://www.ruby-forum.com/t/acts-as-ferret-with-conditions/66435/8 "2006-09-05T17:50:58Z")

</div>

On 25 Aug 2006, at 21:31, Adrian M. wrote:

> find\_by\_contents. That way I learned exactly how it works and  
> adapted it for my needs.
> 
> Hope it helps,

Hi Adrian,

Thanks for the tips, I’ve gone down a “filter afterwards” route which  
is working nicely now.

Cheers,

Alastair

* * *

Alastair M.  
Standards compliant web development with Ruby On Rails, PHP and ASP  
[www.kozmo.co.uk](http://www.kozmo.co.uk)  
07738 399038
