如何按多个字段过滤结果?

发布于 2024-09-01 16:48:41 字数 330 浏览 4 评论 0原文

我正在 ruby​​ on Rails 中开发一个调查应用程序,在结果页面上,我想让用户通过我在调查开始时提出的一系列人口统计问题来过滤答案。

例如,我询问用户的性别和职业是什么。所以我正在考虑设置性别和职业的下拉列表。两个下拉列表都默认为全部,但如果用户选择女性和营销人员,那么我的结果页面将仅来自女性营销人员的答案。

我认为这样做的正确方法是使用named_scopes,其中我对每个人口统计问题都有一个named_scope,在本例中是性别和职业,它将从下拉列表中获取经过净化的值以在有条件时使用,但我'我不确定如何动态创建named_scope链,因为我有大约5个人口统计问题,大概其中一些将被设置为全部。

I am working on a survey application in ruby on rails and on the results page I want to let users filter the answers by a bunch of demographic questions I asked at the start of the survey.

For example I asked users what their gender and career was. So I was thinking of having dropdowns for gender and career. Both dropdowns would default to all but if a user selected female and marketer then my results page would so only answers from female marketers.

I think the right way of doing this is to use named_scopes where I have a named_scope for every one of my demographic questions, in this example gender and career, which would take in a sanitized value from the dropdown to use at the conditional but i'm unsure on how to dynamically create the named_scope chain since I have like 5 demographic questions and presumably some of them are going to be set to all.

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

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

发布评论

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

评论(3

素年丶 2024-09-08 16:48:41

您可以将命名范围链接在一起:

def index
  @results = Results.scoped
  @results = @results.gender(params[:gender]) unless params[:gender].blank?
  @results = @results.career(params[:career]) unless params[:career].blank?
end

但是我更喜欢使用 has_scope gem:

has_scope :gender
has_scope :career

def index
  @results = apply_scopes(Results).all
end

如果您使用 has_scopeinherited_resources< /a>,您甚至不需要定义索引操作。

You can chain named scopes together:

def index
  @results = Results.scoped
  @results = @results.gender(params[:gender]) unless params[:gender].blank?
  @results = @results.career(params[:career]) unless params[:career].blank?
end

I prefer however to use the has_scope gem:

has_scope :gender
has_scope :career

def index
  @results = apply_scopes(Results).all
end

If you use has_scope with inherited_resources, you don't even need to define the index action.

寄人书 2024-09-08 16:48:41
 named_scope :gender,lambda { |*args|
    unless args.first.blank?
      { :conditions => [ "gender = ?", args.first] }   
    end
  }

如果您以这种方式编写命名范围,则可以将它们全部链接起来,并且如果您的参数之一为空,则不会中断。

Result.gender("Male") will return male results.
Result.gender("") will return male and female too.

你可以像这样链接所有的方法。最后作为过滤,你可以有这样的:

Result.age(16).gender("male").career("beginer")
Result.age(nil).gender("").career("advanced") - will return results with advanced career, etc.
 named_scope :gender,lambda { |*args|
    unless args.first.blank?
      { :conditions => [ "gender = ?", args.first] }   
    end
  }

If you write named scopes in this way, you can have all them chained, and if one of your params will be blank wont breaks.

Result.gender("Male") will return male results.
Result.gender("") will return male and female too.

And you can chain all of your methods like this. Finally as a filtering you can have like:

Result.age(16).gender("male").career("beginer")
Result.age(nil).gender("").career("advanced") - will return results with advanced career, etc.
风尘浪孓 2024-09-08 16:48:41

尝试这样的:

VistaFact.where( if active then {:field => :vista2} else {} end)

或者这样的:

VistaFact.where(!data.blank? ? {:field=>data.strip} : {}).where(some? ? {:field2 => :data2} : {}).where ...

这对我来说非常好!

Try some like this:

VistaFact.where( if active then {:field => :vista2} else {} end)

Or like this:

VistaFact.where(!data.blank? ? {:field=>data.strip} : {}).where(some? ? {:field2 => :data2} : {}).where ...

That work for me very nice!

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