SO ...如何将具有关联的活动记录转换为嵌套散列?
我写一些代码,将查询与各协会积极记录,并以类似的格式返回数据...
[{
foo: {
id: ...,
foo_property: ...
},
bar: {
id: ...,
bar_property: ...
}
}, ...]
...顶级活动记录它的组织都有时间戳和我想排除的其他领域。我知道attributes
和as_json
但这些都不遵循关系(而是用foo_id和bar_id替换foo和bar)。我也知道AssociationReflection
和它的能力,动态地找到关联,可以与一些复杂性执行我想要的,但理想...
我想要一个简单,优雅的方式来查询我的数据与此转换和属性过滤,即
Whatever.joins(:foos, :bars)
.eager_load(:foos, :bars)
.? # convert results to an array of hashes with association data (under appropriately named hash key)
.? # filter top level entries and association data to not have timestamps, etc. (select?)
......谢谢!
嗯,你能告诉我们的模型? – niceman