1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

モデルから取ってきた結果が空のとき、rablでnilではなく空配列が返るようにする

Last updated at Posted at 2016-08-08
some.rabl
child(@users, object_root: false) do

とrablで書いてあるときに、

@users = User.where(user_id: params[:user_ids]).includes(:partner_organization_branches)の

結果が

@users = []

となる時、

jsonの結果はusers: nil
となってしまい、普通は配列[]で帰ってくるのにも関わらず、(何故こんな仕様になってるのか謎ですが)nilで返ってくる...

返り値を揃えるために以下のようにrablを変更すればok。

api_response.rabl
if @users.present?
  child() do
    honya_honya
  end
else
  node(:users) { [] }
end
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?