【问题标题】:How can I include more fields to where condition using ransack?如何使用 ransack 将更多字段包含在 where 条件中?
【发布时间】:2012-11-22 18:00:35
【问题描述】:

我正在使用 ransack 谓词构建查询,因此这意味着我需要大量条件用于 SQL 方面的 WHERE 子句。以下是我对 ransanck 的 search 方法的条件哈希:

这是params[:q]的控制台输出

{"price_gteq"=>"0", "rooms_gteq"=>"1", "baths_gteq"=>"1", "has_photos_true"=>"0", "zone_id"=>"1", "deal_type_eq"=>"1", "s"=>{"0"=>{"name"=>"price", "dir"=>"asc"}}, "price_lteq"=>"2500000", "rooms_lteq"=>"2", "baths_lteq"=>"7", "sector_id_in"=>nil}

这是它生成的 SQL 查询:

Property.search(params[:q]).result.to_sql

这给出了:

SELECT "properties".* FROM "properties" LEFT OUTER JOIN "sectors" ON "sectors"."id" = "properties"."sector_id" WHERE (("properties"."price" >= 0.0 AND "properties"."price" <= 2500000.0)) ORDER BY "properties"."price" ASC

如何在where 子句中包含所有字段?

如您所见,只有:price 字段保留在where 子句中。我也需要将:baths:rooms 包含在where 子句中。

我该如何解决?

【问题讨论】:

    标签: ruby-on-rails ruby meta-search ransack


    【解决方案1】:

    幸运的是,我已经解决了自己的问题,发生的事情是我使用 UNRANSACKABLE_ATTRS 数组为我的模型从 排序(Ransack 提供的默认排序方法)中排除上述字段,但是我不知道这也会影响 搜索。无论如何,我删除了 UNRANSACKABLE_ATTRS 数组,搜索中的每个条件都开始按预期工作。 :)

    【讨论】:

      猜你喜欢
      • 2014-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多