【问题标题】:Searching on associations in Spree在 Spree 中搜索关联
【发布时间】:2016-07-30 03:19:01
【问题描述】:

Spree 报告利用搜查来进行搜索。根据 ransack 文档,我应该能够使用以下代码行来搜索所有包含多个项目的订单。

Spree::Order.ransack(line_items_quantity_gt: 1)

但是当我在控制台中输入该行时,我得到以下输出

Ransack::Search<class: Spree::Order, base: Grouping <combinator: and>>

根据应该工作的洗劫文档。搜索参数中似乎不接受该关联。订单项在 Spree::Order.ransackable_associations 提供的列表中,所以我有点困惑为什么这不起作用。

【问题讨论】:

  • 我现在面临同样的问题,按变体 sku 搜索顺序。
  • 我发现了这个问题,这可能有助于他们面对这个问题。我正在查询的关联不在模型的列表whitelisted_ransackable_associations 上。我添加了这个和宾果游戏,它起作用了。
  • 这敲响了我如何最终自己解决此问题的警钟。提交答案,我会接受的
  • 我一直在寻找关联在 api 中不起作用的原因,甚至在带有 rails 控制台的代码中也是如此。一些如何在谷歌中没有看到这个主题。我只通过whitelisted_ransackable_associations 找到它。我在回答中展示了如何准确地覆盖应用程序中的默认值。

标签: ruby-on-rails spree ransack


【解决方案1】:

某些关联(属性、范围)可能会被列入白名单,而其他关联则会被扫荡忽略。

要覆盖应用程序中的默认值添加(到)文件app/controllers/spree/products_controller_decorator.rb

Spree::ProductsController.class_eval do

  Spree::Product.whitelisted_ransackable_associations = %w[taxons stores variants_including_master master variants]

end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 2016-03-28
    • 1970-01-01
    • 2014-05-31
    相关资源
    最近更新 更多