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=%5B17,18%5D” 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.
You have bad format of array in URL string:
Use:
Related topic:
passing arrays as url parameter