【问题标题】:Dynamically include associations for objects in Rails在 Rails 中动态包含对象的关联
【发布时间】:2017-06-06 05:37:30
【问题描述】:

我目前正在开发一个小型 Rails 5 应用程序,我需要根据某些事件将 ActiveRecord 对象传递给外部服务。在我的模型中,我定义了以下内容:

# /models/user.rb
after_create :notify_external_service_of_user_creation

def notify_external_service_of_user_creation
  EventHandler.new(
    event_kind: :create_user,
    content: self
  )
end

EventHandler 然后将此对象转换为 JSON,并通过 HTTP 请求将其发送到外部服务。通过在对象上调用 .to_json 会呈现一个 JSON 输出,如下所示:

{
  "id":1234,
  "email":"test@testemail.dk",
  "first_name":"Thomas",
  "last_name":"Anderson",
  "association_id":12,
  "another_association_id":356
}

现在,我需要一种将所有第一级关联直接包含在其中的方法,而不仅仅是显示foreign_key。所以我正在寻找的结构是这样的:

{
  "id":1234,
  "email":"test@testemail.dk",
  "first_name":"Thomas",
  "last_name":"Anderson",
  "association_id":{
    "attr1":"some_data",
    "attr2":"another_value"
  },
  "another_association_id":{
    "attr1":"some_data",
    "attr2":"another_value"
  },
}

我的第一个想法是像这样反映模型:object.class.name.constantize.reflect_on_all_associations.map(&:name),其中object 是本例中的用户实例,并使用此列表循环关联并将它们包含在输出中。不过这似乎相当乏味,所以我想知道是否有更好的方法可以使用 Ruby 2.4 和 Rails 5 来实现这一点。

【问题讨论】:

标签: ruby-on-rails json ruby ruby-on-rails-5 ruby-2.4


【解决方案1】:

如果您不想使用外部序列化程序,您可以为每个模型覆盖 as_jsonas_jsonto_json 调用。

module JsonWithAssociations
  def as_json
    json_hash = super

    self.class.reflect_on_all_associations.map(&:name).each do |assoc_name|
      assoc_hash = if send(assoc_name).respond_to?(:first)
                     send(assoc_name).try(:map, &:as_json) || [] 
                   else
                     send(assoc_name).as_json 
                   end

      json_hash.merge!(assoc_name.to_s => assoc_hash)
    end 

    json_hash
  end
end

您需要prepend 这个特定模块,以便它覆盖默认的as_json 方法。

User.prepend(JsonWithAssociations)

class User
  prepend JsonWithAssociations
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    • 2015-05-13
    • 1970-01-01
    相关资源
    最近更新 更多