如何在控制器中使用 as_json 到 return 模型的方法 display_name?
How to use as_json in a controller to return a model's method display_name?
在我的用户模型中,我有一个方法display_name来输出用户的全名。
user.rb (id, first_name, last_name, email, ...)
def display_name
[first_name, last_name].compact.join(' ')
end
我正在尝试让我的控制器像这样 return display_name:
def show
json_response({
user: user.as_json(only: [:id, :display_name, :email])
})
end
问题是,控制器只发送 ID 和电子邮件,而不是 display_name..我做错了什么?
根据 docs:
To include the result of some method calls on the model use :methods:
user.as_json(methods: :permalink)
# => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
# "created_at" => "2006/08/01", "awesome" => true,
# "permalink" => "1-konata-izumi" }
另外:
The option include_root_in_json controls the top-level behavior of as_json. If true, as_json will emit a single root node named after the object’s type. The default value for include_root_in_json option is false.
user = User.find(1)
user.as_json
# => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
# "created_at" => "2006/08/01", "awesome" => true}
ActiveRecord::Base.include_root_in_json = true
user.as_json
# => { "user" => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
# "created_at" => "2006/08/01", "awesome" => true } }
This behavior can also be achieved by setting the :root option to true as in:
user = User.find(1)
user.as_json(root: true)
# => { "user" => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
# "created_at" => "2006/08/01", "awesome" => true } }
所以,大概是这样的:
user.as_json(only: [:id, :email], methods: :display_name, root: true)
你可以这样做:
user.as_json(only: [:id, :email], methods: [:display_name])
或
user.as_json(only: [:id, :email]).merge({'display_name': user.display_name})
或者有一个序列化器
在我的用户模型中,我有一个方法display_name来输出用户的全名。
user.rb (id, first_name, last_name, email, ...)
def display_name
[first_name, last_name].compact.join(' ')
end
我正在尝试让我的控制器像这样 return display_name:
def show
json_response({
user: user.as_json(only: [:id, :display_name, :email])
})
end
问题是,控制器只发送 ID 和电子邮件,而不是 display_name..我做错了什么?
根据 docs:
To include the result of some method calls on the model use :methods:
user.as_json(methods: :permalink)
# => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
# "created_at" => "2006/08/01", "awesome" => true,
# "permalink" => "1-konata-izumi" }
另外:
The option include_root_in_json controls the top-level behavior of as_json. If true, as_json will emit a single root node named after the object’s type. The default value for include_root_in_json option is false.
user = User.find(1)
user.as_json
# => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
# "created_at" => "2006/08/01", "awesome" => true}
ActiveRecord::Base.include_root_in_json = true
user.as_json
# => { "user" => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
# "created_at" => "2006/08/01", "awesome" => true } }
This behavior can also be achieved by setting the :root option to true as in:
user = User.find(1)
user.as_json(root: true)
# => { "user" => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
# "created_at" => "2006/08/01", "awesome" => true } }
所以,大概是这样的:
user.as_json(only: [:id, :email], methods: :display_name, root: true)
你可以这样做:
user.as_json(only: [:id, :email], methods: [:display_name])
或
user.as_json(only: [:id, :email]).merge({'display_name': user.display_name})
或者有一个序列化器