acts_as_ferret with conditions

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

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

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])

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 Madrid

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,

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 Madrid

Hi Adrian,

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

Cheers,

Alastair

Alastair,

Glad to help.

Sincerely,

Adrian Madrid