We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
モデルのインスタンスを Serializer に与えて to_json するだけで良さげ
class UserSerializer < ActiveModel::Serializer attributes :id, :name end
空のインスタンス
user = User.new # <User:0x000055cfd1dcf6f8 # id: nil, # name: nil, # email: nil, # created_at: nil, # updated_at: nil> serialized_user = UserSerializer.new(user) serialized_user.to_json # => "{\"id\":null,\"name\":null}"
DBからインスタンスを得る例
user = User.last # #<User:0x000055cfd1ea0730 # id: 19, # name: "吾輩", # created_at: Fri, 20 Mar 2020 01:02:10 UTC +00:00, # updated_at: Fri, 20 Mar 2020 01:02:10 UTC +00:00> serialized_user = UserSerializer.new(user) serialized_user.to_json # => => "{\"id\":19,\"name\":\"吾輩\"}"
The text was updated successfully, but these errors were encountered:
No branches or pull requests
モデルのインスタンスを Serializer に与えて to_json するだけで良さげ
Serializer
動作
空のインスタンス
DBからインスタンスを得る例
The text was updated successfully, but these errors were encountered: