Last active
October 3, 2016 02:52
-
-
Save axtutuu/d0fe5590a87caf77bd2eb774076a3850 to your computer and use it in GitHub Desktop.
ActiveRecordで取得したデータをカスタマイズしてJSONで返却
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 Model < ActiveRecord::Base | |
has_many :users | |
def as_json(options={}) | |
super.as_json(options).merge({users: self.users}) | |
end | |
end |
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 ModelController < ApplicationController | |
# usersを含めたjsonの返却 | |
def index | |
render json: { data: Model.all } | |
end | |
end |
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 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