I have this http://localhost:3000/api/products request that returns json format output with a list of products objects. I heard in some scenario its better to use multi-gets design where you list products ids in URI like so http://localhost:3000/api/products?ids=1,2,3,4. My question is how do i configure the route to return such an URI? Below is the json output returned by http://localhost:3000/api/products.Thank you in advance
controller
def index
@products=Product.all
respond_to do |format|
format.json { render json: @products.to_json}
format.xml
end
end
json output
[
{
"category_id": null,
"created_at": "2011-03-25T13:35:16Z",
"details": "Molestias pariatur consequuntur ut voluptas aperiam facere et et autem ad laudantium ut qui dolorem iste sit ut in dignissimos. Et debitis et et sunt quidem qui est est et numquam in dolorum natus sapiente nihil ipsa ratione. Quisquam aut molestiae earum voluptas vero et officiis magnam quam provident voluptatibus quia",
"id": 1,
"product_name": "Velit",
"publisher_id": 1,
"updated_at": "2012-11-12T18:45:13Z",
"publisher_details": "http://localhost:3000/api/users/1"
},
{
"category_id": null,
"created_at": "2012-01-10T23:16:53Z",
"details": "Temporibus quis et quam eveniet hic consequatur maiores eum expedita molestiae velit eligendi laboriosam ut molestiae. Velit delectus aliquid nobis quia velit aut dolorem omnis numquam reprehenderit quo illo saepe molestiae nisi. Soluta nihil quae soluta facilis cumque voluptates eaque amet unde non in placeat id cupiditate illum at et vero. Laborum id eaque voluptas illo eius iure",
"id": 2,
"product_name": "Nam Laboriosam Et Sed",
"publisher_id": 1,
"updated_at": "2012-11-12T18:45:13Z",
"publisher_details": "http://localhost:3000/api/users/1"
}]
There is a gem, which is discontinued, which was written for sproutcore (now ember) that offered this out of the box: https://github.com/drogus/bulk_api
But actually, it does not have to be that hard at all. For a single controller I would just do something like
Does that help?