【发布时间】:2018-07-15 00:39:25
【问题描述】:
所以,Object 有许多 properties,而 property 属于 Object。这就是目前的协会。现在,当我使用 Object.all 渲染所有对象并包含 :properties 关联时,我在渲染的 JSON 中获得了 property 模型的所有属性。但我真的不需要那个。我需要的只是数量的关联。即,只有与任何一个对象相关联的属性数量。这是我的代码:
@objects = Object.all
respond_to do |format|
format.json {
render json: @objects.to_json(:include => [:properties])
}
end
这会产生类似的结果。目前只有 1 个property 与现有的 1 个object 相关联,正如您在 JSON 中看到的那样,我得到了很多关于我没有关联的properties(或者更确切地说是属性)的信息不需要也不想要。
"id": 2,
"attrs_go_here": "asdfsdaf"
"created_at": "2018-07-14T23:51:55.161Z",
"updated_at": "2018-07-14T23:51:55.161Z",
"properties": [
{
"id": 5,
"more_attributes": "asdfasdfasdf"
"created_at": "2018-07-14T23:53:14.917Z",
"updated_at": "2018-07-14T23:53:14.917Z"
}
]
但是,相反,我想要这样的东西,我得到关联的数量:
"id": 2,
"attrs_go_here": "asdfsdaf"
"created_at": "2018-07-14T23:51:55.161Z",
"updated_at": "2018-07-14T23:51:55.161Z",
"properties": 1
我只想要这个数字的原因是因为可能有数十万个属性与任何一个对象相关联。当我首先需要的是对象与其属性之间的关联数量时,我觉得渲染与对象关联的所有属性的所有属性真的很慢。
【问题讨论】:
-
可以添加属性表的架构吗?
标签: ruby-on-rails json api