【发布时间】:2017-03-13 08:12:08
【问题描述】:
所以...
我正在编写一些代码,它将查询具有关联的活动记录并以类似于...的格式返回数据...
[{
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?)
...谢谢!
【问题讨论】:
-
hmmm 你能给我们看看模型吗?
标签: ruby-on-rails ruby activerecord rails-activerecord