将模型方法的返回哈希转换为可序列化哈希(用于as_json)。

huangapple go评论95阅读模式
英文:

Convert a returning Hash of a Model's method to a serializable_hash (for as_json)

问题

你可以尝试在Controller中使用 as_json 方法,并结合 :only:except 来过滤 child_data 方法返回的哈希数据,如下所示:

  1. class Api::V5::Private::FamilyController < Api::V5::PrivateController
  2. def index
  3. families = Family.all.map do |family|
  4. {
  5. id: family.id,
  6. last_name: family.last_name,
  7. child_data: family.child_data.slice(:first_name, :gender)
  8. }
  9. end
  10. render json: families
  11. end
  12. end
英文:

My Modelclass has a method, that returns some calculated data in form of a Hash. In the Controller I want to use this data, but want to include only some parts of the Hash.

My first idea was, to use the method inside :include in the to_json-options instead of the :methods field. But this will end in an undefined method 'serializable_hash'-Error.

Model:

  1. class Family < ActiveRecord::Base
  2. def child_data
  3. {
  4. gender: self.child_gender,
  5. first_name: self.child_first_name,
  6. last_name: self.child_last_name,
  7. email: self.child_email,
  8. phone: self.child_phone
  9. }
  10. end
  11. end

Controller:

  1. class Api::V5::Private::FamilyController < Api::V5::PrivateController
  2. def index
  3. render json: Family.all.to_json({
  4. only: [ :id, :last_name ],
  5. # methods: [ :child_data ], <-- WOULD WORK
  6. include: {
  7. child_data: {
  8. only: [ :first_name, :gender ]
  9. }
  10. }
  11. })
  12. end
  13. end

How can I use the returning Hash of the method "child_data" inside to_json/as_json, but use :only and :except to filter the Hash?

答案1

得分: 0

以下是已翻译的部分:

  1. 使用一个具有方法 "serializable_hash" 的模块,并扩展哈希对象以使用该模块:
  1. module Serializable
  2. def serializable_hash opts
  3. self.as_json(opts)
  4. end
  5. end

  1. def child_data
  2. {
  3. gender: self.child_gender,
  4. first_name: self.child_first_name,
  5. last_name: self.child_last_name,
  6. email: self.child_email,
  7. phone: self.child_phone
  8. }.extend(Serializable)
  9. end
  1. 在 "initializers/" 中重新打开 Ruby 的哈希类并添加方法:

config/initializers/hash_extensions.rb

  1. class Hash
  2. def serializable_hash opts
  3. self.as_json(opts)
  4. end
  5. end

我将只返回翻译好的部分,不提供额外的内容。

英文:

Found two solutions for this:

  1. Use a module with method "serializable_hash" and extend the Hash with this module:
  1. module Serializable
  2. def serializable_hash opts
  3. self.as_json(opts)
  4. end
  5. end

and

  1. def child_data
  2. {
  3. gender: self.child_gender,
  4. first_name: self.child_first_name,
  5. last_name: self.child_last_name,
  6. email: self.child_email,
  7. phone: self.child_phone
  8. }.extend(Serializable)
  9. end
  1. Extend Ruby's Hash Class inside the "initializers/" by reopening the Class and add the method:

config/initializers/hash_extensions.rb

  1. class Hash
  2. def serializable_hash opts
  3. self.as_json(opts)
  4. end
  5. end

I'll leave this here for anybody, who also got into this problem.

huangapple
  • 本文由 发表于 2023年6月27日 18:48:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定