简体   繁体   English

使用具有habtm关联的searchlogic

[英]using searchlogic with habtm associations

I have the following issue: 我有以下问题:

I have two classes in my rails app, joined by a HABTM association, like so: 我的rails应用程序中有两个类,并由HABTM协会加入,如下所示:

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

joined by an ads_specs table. 由ads_specs表加入。 I'm trying to perform a search on the Ad class using the excellent searchlogic gem. 我正在尝试使用出色的searchlogic宝石对Ad类进行搜索。 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". 我尝试了@ads = Ad.specs_id_like_all(id1, id2, id3)但没有结果,因为我认为它试图匹配ID为“ id1,id2,id3”的规范。 I also tried to .split the ids or directly type them in an array but nothing worked. 我也试图.id拆分id或直接在数组中键入它们,但是没有任何效果。

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 :). 我发誓这是我最后一次在Rails应用程序中使用HABTM关联,但是现在在开发过程的后期才改变为多态的为时已晚:)。

If i understand correctly, you're trying to perform an SQL IN() query (ie spec_id IN (x,y,z..) ) 如果我正确理解,则您正在尝试执行SQL IN()查询(即spec_id IN (x,y,z..)

Searchlogic does support passing an array to a *_eq() method which will use SQL's IN() predicate: Searchlogic确实支持将数组传递给*_eq()方法,该方法将使用SQL的IN()谓词:

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

**Not sure if this works in older versions of Searchlogic* **不确定在旧版Searchlogic *中是否可行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM