通过 Rails 查询具有多个下拉菜单的单个下拉菜单
我在解决以下问题时遇到一些困难。如果这篇文章有点令人困惑,我深表歉意,但我会尽力解释它。
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您同时设置了
:state_1
和:state_2
,那么您将执行以下操作:这就是说,
每次调用
.where
时,您正在附加另一个带有连词(即 AND)的条件。我认为你在追求这个:这将变成 SQL 有点像这样:
这似乎就是你在追求的。
map
将提取 < code>param 值用于指定的状态键,然后是reject
将过滤掉 空白(即false
、nil
、空或全是空格)。If you have both
:state_1
and:state_2
set, then you'll be doing this:And that's saying
Each time you call
.where
, you're appending another condition with a conjunction (i.e. AND). I think you're after this:That will turn into SQL sort of like this:
and that seems to be what you're after. The
map
will extract theparam
values with for the specified state keys and then thereject
will filter out the blank ones (i.e.false
,nil
, empty, or all whitespace).