简体   繁体   English

在搜袋式搜索过滤器中包括选定的has_scope

[英]Include selected has_scope on ransack search filter

I am attempting to combine has_scope and ransack. 我试图结合has_scope和ransack。

Each of these components is working perfectly by themselves. 这些组件中的每一个都可以完美地运行。 However, when I attempt to combine them, they overwrite each other. 但是,当我尝试将它们组合时,它们会相互覆盖。 For example, if I select a scope, the results are appropriately filtered, but once I use the search_form from ransack to filter the results further, the scope is then removed. 例如,如果我选择范围,则会对结果进行适当过滤,但是一旦我使用ransack中的search_form进一步过滤结果,则会删除范围。 The inverse is also true. 反之亦然。

How can this be achieved? 怎么能实现这一目标?

Thanks for your help. 谢谢你的帮助。

Please see my attempt below. 请看下面我的尝试。

has_scope :upward_trending, :type => :boolean
has_scope :downward_trending, :type => :boolean
has_scope :all, :type => :boolean

def index
    @has_scope = apply_scopes(Product).all
    @q = @has_scope.search(params[:q])
    @products = apply_scopes(@q.result.page(params[:page]).per(30))
end

With Ransack's new ransackable_scopes functionality, there is no longer a need for has_scope 使用Ransack的新ransackable_scopes功能,不再需要has_scope

I can do it like this: 我可以这样做:

def self.ransackable_scopes(auth_object = nil)
    [:upward_trending, :downward_trending, :seven_days, :thirty_days, :six_months, :twelve_months, :all_time]
end

And then I am able to call these scopes within Ransack as needed. 然后我可以根据需要在Ransack内调用这些范围。

So to answer the question, with this new functionality, the scopes are already integrated and we are all good. 因此,要回答这个问题,借助这一新功能,范围已经集成,我们都很好。

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

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