Skip to content

Instantly share code, notes, and snippets.

@yutackall
Created February 6, 2015 08:17
Show Gist options
  • Save yutackall/3d97262da31b3f12a15c to your computer and use it in GitHub Desktop.
Save yutackall/3d97262da31b3f12a15c to your computer and use it in GitHub Desktop.
Rails で 指定した ID 順に取得する ref: http://qiita.com/yutackall/items/34cf4f1dee10aa10821a
class UsersController < ApplicationController
# GET /users/ids
def ids
@users = User.all.order(accessced_at: :desc)
render json: @users.pluck(:id)
end
# GET /users/lookup?ids[]=1&ids[]=2
def lookup
@users = User.where(id: params[:ids]).sort_by{|c| params[:ids].map(&:to_i).index(c.id)}
# view が用意されている前提です。
# app/views/users/lookup.json.jbuilder
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment