Jbuilder 包括所有属性和关联

Jbuilder include All Attributes and Associations

我正在尝试设置一个我可以克隆的 JSON 对象,因此我需要 Jbuilder 列出所有对象属性和关联。我已经使用 Jbuilder 做了很多选择 JSON 结构,但似乎无法找到一种简单的方法来一次性列出所有内容而不显式声明所有内容。

这适用于单个 company 属性和所有关联的 location 属性...

//show.json.jbuilder

company = @company

json.company do
  json.(company, :id)
  json.locations_attributes company.locations
end

但我想包括所有 company 属性,如果不列出所有属性似乎无法做到这一点 json.(company, :id, :name, :line_of_business)

好吧,你可以用ActiveRecord #attributes 的方法来看起来整齐

company = @company

json.company do
  json.(company, *company.attributes.keys)
  json.locations_attributes company.locations
end

或如this答案中所述:

json.merge! company.attributes