Reputation: 1974
I need to fetch several ids in one GET request like
http://localhost:3000/api/positions/ids
I've tried some ways to do this, but no one worked:
This returned only first object:
http://localhost:3000/api/positions/1,2
this
http://localhost:3000/api/positions?id=1,2
and that
http://localhost:3000/api/positions?id=1&id=2
returned all objects, but first and second.
How can I do it? Thanks!
Upvotes: 0
Views: 645
Reputation: 32955
The syntax for arrays in parameters is id[]=1&id[]=2&id[]=3
but if you have large numbers of ids then this can become quite cumbersome and ugly. I would suggest that you use the parameter id
for a single id and a separate parameter ids
which takes a hyphen-seperated single string, eg
#get a single resource
/api/positions?id=123
#get a list of resources
/api/positions?ids=123-67-456-1-3-5
Now you can make your controller code something like this:
if params[:id]
@foos = Foo.find_all_by_id(params[:id])
elsif params[:ids]
@foos = Foo.find_all_by_id(params[:ids].split("-"))
end
Upvotes: 2