将 searchlogic 与 habtm 关联结合使用

发布于 2024-09-06 15:52:09 字数 853 浏览 10 评论 0原文

我有以下问题:

我的 Rails 应用程序中有两个类,由 HABTM 关联加入,如下所示:

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

由 ads_specs 表加入。 我正在尝试使用优秀的 searchlogic gem 对 Ad 类执行搜索。一切都很顺利,直到我想返回具有所有选定规格的所有广告,而不是其中任何一个。

我尝试了 @ads = Ad.specs_id_like_all(id1, id2, id3) 但没有结果,因为我认为它试图匹配 id 为“id1, id2, id3”的规范。我还尝试了 .split id 或直接将它们键入数组中,但没有任何效果。

我的确切搜索查询是:

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

你们知道我该如何解决这个问题吗?我发誓这是我最后一次在 Rails 应用程序中使用 HABTM 关联,​​但在开发过程的后期更改为多态关联已经太晚了:)。

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 :).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙女 2024-09-13 15:52:09

如果我理解正确,您正在尝试执行 SQL IN() 查询(即 spec_id IN (x,y,z..)

Searchlogic 确实支持将数组传递给 * _eq() 方法将使用 SQL 的 IN() 谓词:

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

**不确定这是否适用于旧版本的 Searchlogic*

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*

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文