# Using conditions

**URL:** <https://www.ruby-forum.com/t/using-conditions/66443>\
**Category:** Ferret\
**Created:** [August 25, 2006, 4:02pm UTC](https://www.ruby-forum.com/t/using-conditions/66443 "2006-08-25T16:02:10Z")\
**Posts on this page:** 5\
**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, 4:02pm UTC](https://www.ruby-forum.com/t/using-conditions/66443/1 "2006-08-25T16:02:10Z")

</div>

Hello guys,

I’m pretty new to using AAF and am having a slight problem with 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

---

<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:31pm UTC](https://www.ruby-forum.com/t/using-conditions/66443/2 "2006-08-25T17:31:16Z")

</div>

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

> Cheers,
> 
> Alastair

I’m not sure if the find\_by\_contents method has a :contents parameter  
but even if it does, I’d personally do it just like this;

```
@results = SupplierProduct.find_by_contents(params[:search] + "

```

+area\_id:#@area")

cheers,  
Dave

---

<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:56am UTC](https://www.ruby-forum.com/t/using-conditions/66443/3 "2006-08-26T11:56:51Z")

</div>

On Sat, Aug 26, 2006 at 01:29:17AM +1000, David B. wrote:

> > pointers?
> > 
> > Cheers,
> > 
> > Alastair
> 
> I’m not sure if the find\_by\_contents method has a :contents parameter  
> but even if it does, I’d personally do it just like this;
> 
> ```
> @results = SupplierProduct.find_by_contents(params[:search] + "
> 
> ```
> 
> +area\_id:#@area")

that of course works if :area\_id is an indexed field.

If area\_id is not indexed, the :conditions option can be used to limit  
the find call that retrieves the model instances via ActiveRecord. Note  
that you might get less results than expected when using :conditions,  
since AR filters records out that Ferret counted as a hit.

correct usage:

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

the first hash is for ferret-related options (like :limit, :offset and  
so on), the second for activeRecord options.

Jens

–  
webit! Gesellschaft für neue Medien mbH [www.webit.de](http://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

---

<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, 9:49pm UTC](https://www.ruby-forum.com/t/using-conditions/66443/4 "2006-09-05T21:49:29Z")

</div>

Jens K. wrote:

> that of course works if :area\_id is an indexed field.
> 
> If area\_id is not indexed, the :conditions option can be used to limit  
> the find call that retrieves the model instances via ActiveRecord. Note  
> that you might get less results than expected when using :conditions,  
> since AR filters records out that Ferret counted as a hit.
> 
> correct usage:
> 
> @results = SupplierProduct.find\_by\_contents(params[:search], {}, {  
> :conditions =\> [] })
> 
> the first hash is for ferret-related options (like :limit, :offset and  
> so on), the second for activeRecord options.

Hi Jens and thanks for the help but I seem to be getting an error when I  
use the following -

@results = MarketingProduct.find\_by\_contents(search\_text, {},  
:conditions =\> ‘live = “t”’)

I’m getting this error -

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occured while evaluating nil.sort!

#{RAILS\_ROOT}/vendor/plugins/acts\_as\_ferret/lib/acts\_as\_ferret.rb:286:in  
`find_by_contents' #{RAILS_ROOT}/app/controllers/marketing_controller.rb:21:in`results’

Any ideas what might be up?

Thanks,

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:** [September 6, 2006, 10:35am UTC](https://www.ruby-forum.com/t/using-conditions/66443/5 "2006-09-06T10:35:32Z")

</div>

Hi!  
On Tue, Sep 05, 2006 at 09:49:29PM +0200, Alastair M. wrote:

> > 
> 
> @results = MarketingProduct.find\_by\_contents(search\_text, {},  
> :conditions =\> ‘live = “t”’)

that should read :conditions =\> [“live=‘t’”] . the clause gets shifted  
from the array and ANDed with aaf’s own where clause, and any additional  
parameters get added to the conditions array that is the issued to  
ActiveRecord.

But that doesn’t seem to be your exact problem… What version of aaf do  
you use ? From the line number that doesn’t seem to be latest trunk or  
the last  
version tagged stable ?

cheers,  
Jens

> Any ideas what might be up?  
> [http://rubyforge.org/mailman/listinfo/ferret-talk](http://rubyforge.org/mailman/listinfo/ferret-talk)  
> –  
> webit! Gesellschaft für neue Medien mbH [www.webit.de](http://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
