Rails 3.1 查找通过 AJAX 发送的 ID 数组过滤的记录

发布于 2024-12-17 20:58:25 字数 715 浏览 2 评论 0原文

我有两个表:项目和供应。项目有很多供应,如下所示:

has_many :supplies, :through => :project_supplies, :foreign_key => :supply_id

我想做的一件事是根据一系列供应 id 选择项目。我可以通过以下方式在 Rails 控制台中相当轻松地完成此操作:

Project.find(:all, :joins => :supplies, :conditions => {:supplies => {:id => [17,18]}})

作为一个 Rails 菜鸟,我似乎无法弄清楚如何将该数组作为参数发送,和/或在后端序列化它。

我正在尝试以 Rails 可以理解的方式执行“GET http://0.0.0.0:3000/projects?supply_ids=[17,18]”。

这就是我应该发送数组的方式吗?如果是这样,如何将其以数组形式放入条件子句中?

我还应该提到我正在使用 Rails 3.1 和backbone.js。 get 请求将来自骨干 fetch() 函数,该函数接受“data”选项,就像 $.ajax 一样。我已经在前端构建了数组。

总而言之,我的问题是:JavaScript 数据对象属性到底应该包含什么内容,以及如何将其连接到控制器模型的查找函数(如上所示)?

提前致谢。

I have two tables: projects and supplies. Projects have many supplies, like this:

has_many :supplies, :through => :project_supplies, :foreign_key => :supply_id

One thing that I am trying to do is to select projects based on an array of supply id's. I can do this in the rails console fairly easily with the following:

Project.find(:all, :joins => :supplies, :conditions => {:supplies => {:id => [17,18]}})

Being somewhat of a Rails noob, I can't seem to figure out how to go about sending that array as a parameter, and / or serializing it on the back-end.

I'm trying to do this "GET http://0.0.0.0:3000/projects?supply_ids=[17,18]" in a way that rails understands.

Is that how I'm supposed to send the array? If so, how do I get that into the conditions clause in array form?

I should also mention that I'm using Rails 3.1 and backbone.js. The get request will come from the backbone fetch() function which accepts a "data" option just like $.ajax. I already have the array built on the front-end.

To summarize, my question is this: What exactly should go into the JavaScript data object attribute, and how do I wire that up to the controller model's find function as shown above?

Thanks in advance.

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

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

发布评论

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

评论(1

长梦不多时 2024-12-24 20:58:25

URL 字符串中的数组格式错误:
用途:

GET http://0.0.0.0:3000/projects?supply_ids[]=17&supply_ids[]=18

相关主题:
将数组作为 URL 参数传递

You have bad format of array in URL string:
Use:

GET http://0.0.0.0:3000/projects?supply_ids[]=17&supply_ids[]=18

Related topic:
passing arrays as url parameter

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