Created
May 19, 2011 19:27
-
-
Save dhh/981520 to your computer and use it in GitHub Desktop.
bulk api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bulk API design | |
# | |
# resources :posts | |
class PostsController < ActiveController::Base | |
# GET /posts/1,4,50,90 | |
# post_url([ @post, @post ]) | |
def show_many | |
@posts = Post.find(params[:ids]) | |
end | |
# PUT /posts/1,4,50,90 | |
def update_many | |
@posts = Post.find(params[:ids]) | |
Post.transaction do | |
@posts.each do |post| | |
post.update_attributes!(params[:posts][post.id]) | |
end | |
end | |
end | |
# POST /posts/many | |
def create_many | |
end | |
# DELETE /posts/1,4,50,90 | |
def destroy_many | |
end | |
end |
If I recall correctly, Richardson's Restful Web Services recommends ,
for dimensional things (like /locations/lat,long/
) and ;
for lists of things (like /posts/1;2;3
). Allamaraju's Restful Web Services Cookbook, on the other hand, doesn't express a preference between the two.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@grimen I too wonder if handling the one-or-many cases using a single action makes sense. I'm experimenting with this before_filter:
Is it surprising that params[:id] is always the first of many IDs? Aside from the obvious case of overwriting an explicitly passed :ids parameter, when would this cause problems? Anyhow, here's how I make use of it in a trivial #update example.
I've created a little Rails app with some different examples and tests.
https://github.com/jmorton/bulk-example/blob/tolerant/app/controllers/posts_controller.rb
https://github.com/jmorton/bulk-eaxample/blob/strict/app/controllers/posts_controller.rb