views:

57

answers:

1

Hi guys, I have the following issue:

I have two classes in my rails app, joined by a HABTM association, like so:

#ad.rb
has_and_belongs_to_many :specs
#spec.rb
has_and_belongs_to_many :ads

joined by an ads_specs table. I'm trying to perform a search on the Ad class using the excellent searchlogic gem. Everything went fine until I wanted to return all the ads that have ALL the selected specs, not any of them.

I tried @ads = Ad.specs_id_like_all(id1, id2, id3)but to no results, since I think it's trying to match a spec with an id of "id1, id2, id3". I also tried to .split the ids or directly type them in an array but nothing worked.

My exact search query is:

if params[:search]
  @ads = Ad.search(:price_lte => params[:search][:price],
                   :rulaj_lte => params[:search][:rulaj],
                   :fabrication_date_gte => Date.new(params[:search][:"an_fabr(1i)"].to_i,"01".to_i,"01".to_i)).specs_id_like_all(2, 457, 233)

else
  @ads = Ad.all
end

Do you guys have any idea how could I solve this problem? I swear it's the last time I use HABTM associations in a rails app, but it's so late to change to a polymorphic one this late in the development process :).

A: 

If i understand correctly, you're trying to perform an SQL IN() query (i.e. spec_id IN (x,y,z..) )

Searchlogic does support passing an array to a *_eq() method which will use SQL's IN() predicate:

Ad.search(:spec_id_eq => [1,2,3,4])

*Not sure if this works in older versions of Searchlogic