通过 Rails 查询具有多个下拉菜单的单个下拉菜单

发布于 2024-11-27 07:11:08 字数 1082 浏览 0 评论 0原文

我在解决以下问题时遇到一些困难。如果这篇文章有点令人困惑,我深表歉意,但我会尽力解释它。

我对 Rails 比较陌生,我正在尝试在我的网站上实现查询。我网站的一部分允许用户在填写表单时从下拉菜单中选择一个州。

站点的另一部分(实现查询的部分)允许用户根据与其关联的状态来查询表单。该页面本质上只有 4 个下拉框和一个提交按钮。此查询应允许用户通过从可用下拉菜单中选择所需的州来查找来自多个州的表单。这意味着用户可以查询 4 ​​个不同状态的表单,并且它将返回表单上的状态与四个下拉菜单之一中的状态匹配的所有表单。

因此,用户可以从 state_1(下拉列表名称)中选择“乔治亚”,从 state_2 中选择“德克萨斯”,从 state_3 中选择“加利福尼亚”,从 state_4 中选择“俄亥俄”。用户提交搜索后,我想返回与用户的下拉选择相匹配的所有可用表单。

我目前遇到了一些问题。这是我当前拥有的代码:

 def self.search(params)
    form = self.scoped

    form = form.where(:state => params[:state_1]) if params[:state_1].present?
    form = form.where(:state => params[:state_2]) if params[:state_2].present?
    form = form.where(:state => params[:state_3]) if params[:state_3].present? 
    form = form.where(:state => params[:state_4]) if params[:state_4].present?

    form
  end

我遇到的问题如下:如果我仅在 state_1 上进行选择运行查询,那么它将返回与该状态关联的所有字段(这非常好);但是,如果我尝试在多个下拉菜单上进行选择运行查询(例如,state_1 上的“Georgia”和 state_2 上的“Texas”),则不会返回任何表单。

如果这让您感到困惑,我再次表示诚挚的歉意,并感谢您抽出时间。如果您有任何帮助,我将不胜感激,并且我将尽力尽快回答任何问题。太感谢了!

I'm having some difficulty figuring out the following problem. I apologize if this writeup is slightly confusing, but I'll try to explain it as best as I can.

I'm relatively new to rails, and I'm trying to implement a query on my site. One portion of my site allows users to select a State from a dropdown menu when filling out a form.

Another portion of the site, (the portion that implements the query) allows users to query for a form based on the State associated with it. The page is essentially only 4 dropdown boxes, and a submit button. This query should allow users to find forms from multiple States by selecting the desired States from the available dropdown menus. Meaning a user can query for forms from 4 different states, and it would return all forms where the state on the form matches the state from one of the four dropdown menus.

So, a user can select "Georgia" from state_1 (name of dropdown), "Texas" from state_2, "California" from state_3, and "Ohio" from state_4. Once a user submits their search, I would like to return all the forms available that match the user's dropdown selections.

I'm running into some issues at present. here is the code I currently have:

 def self.search(params)
    form = self.scoped

    form = form.where(:state => params[:state_1]) if params[:state_1].present?
    form = form.where(:state => params[:state_2]) if params[:state_2].present?
    form = form.where(:state => params[:state_3]) if params[:state_3].present? 
    form = form.where(:state => params[:state_4]) if params[:state_4].present?

    form
  end

The issue i am running into is the following: If I run a query with only a selection on state_1, then it returns all of the fields associated with that state (which is excellent); however, if I try to run a query with selections on multiple dropdown menus (for example, "Georgia" on state_1 and "Texas" on state_2) no forms are returned.

Again, I sincerely apologize if this is confusing, and I'm grateful for your time. I would appreciate any help and I'll do my best to respond to any questions as prompt as possible. Thank you so much!

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

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

发布评论

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

评论(1

无戏配角 2024-12-04 07:11:08

如果您同时设置了 :state_1:state_2 ,那么您将执行以下操作:

form = form.where(:state => params[:state_1]).where(state => params[:state_2])

这就是说,

state = 'TX' AND state = 'GA'

每次调用 .where 时,您正在附加另一个带有连词(即 AND)的条件。我认为你在追求这个:

states = [:state_1, :state_2, :state_3, :state_4].map { |k| params[k] }.reject(&:blank?)
form = form.where(:state => states) if(!states.empty?)

这将变成 SQL 有点像这样:

state IN ('TX', 'GA')

这似乎就是你在追求的。 map 将提取 < code>param 值用于指定的状态键,然后是 reject 将过滤掉 空白(即falsenil、空或全是空格)。

If you have both :state_1 and :state_2 set, then you'll be doing this:

form = form.where(:state => params[:state_1]).where(state => params[:state_2])

And that's saying

state = 'TX' AND state = 'GA'

Each time you call .where, you're appending another condition with a conjunction (i.e. AND). I think you're after this:

states = [:state_1, :state_2, :state_3, :state_4].map { |k| params[k] }.reject(&:blank?)
form = form.where(:state => states) if(!states.empty?)

That will turn into SQL sort of like this:

state IN ('TX', 'GA')

and that seems to be what you're after. The map will extract the param values with for the specified state keys and then the reject will filter out the blank ones (i.e. false, nil, empty, or all whitespace).

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