Rails 3.1 查找通过 AJAX 发送的 ID 数组过滤的记录
我有两个表:项目和供应。项目有很多供应,如下所示:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
URL 字符串中的数组格式错误:
用途:
相关主题:
将数组作为 URL 参数传递
You have bad format of array in URL string:
Use:
Related topic:
passing arrays as url parameter