Skip to content

Instantly share code, notes, and snippets.

@axtutuu
Last active October 3, 2016 02:52
Show Gist options
  • Save axtutuu/d0fe5590a87caf77bd2eb774076a3850 to your computer and use it in GitHub Desktop.
Save axtutuu/d0fe5590a87caf77bd2eb774076a3850 to your computer and use it in GitHub Desktop.
ActiveRecordで取得したデータをカスタマイズしてJSONで返却
class Model < ActiveRecord::Base
has_many :users
def as_json(options={})
super.as_json(options).merge({users: self.users})
end
end
class ModelController < ApplicationController
# usersを含めたjsonの返却
def index
render json: { data: Model.all }
end
end
class User < ActiveRecord::Base
belongs_to :model
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment