Created
February 6, 2015 08:17
-
-
Save yutackall/3d97262da31b3f12a15c to your computer and use it in GitHub Desktop.
Rails で 指定した ID 順に取得する ref: http://qiita.com/yutackall/items/34cf4f1dee10aa10821a
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
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