我有一个关联为 download has_many download_links
的模型我正在downloadlink控制器中调用download_links。
def download_links
@downloads = Download.all
render json: { data:@downloads.as_json(:only=>[:id],
include: {
:downloadlink => {:only =>:url}
}
), message:"success"}, status: :ok
end
邮递员的结果如下:
{
"data": [
{
"id": 1,
"downloadlink": [
{
"url": "1920x1080.png"
},
{
"url": "1366x766.png"
},
{
"url": "1280x720.png"
},
{
"url": "1024x768.png"
}
]
}
],
"message": "success"}
现在我希望结果是这样的:
{
"data": [
{
"id": 1,
"downloadlink": [
"1920x1080.png",
"1366x766.png",
"1280x720.png",
"1024x768.png"
]
}
],
"message": "success"}
答案 0 :(得分:0)
考虑| ID | CREATED | ...
| 1 | 2019-11-01 14:19:28.611000 | ...
| 2 | 2019-11-01 14:19:28.680000 | ...
| 3 | 2019-11-01 14:19:28.612000 | ...
| 4 | 2019-11-01 14:19:28.613000 | ...
| 5 | 2019-11-01 14:19:28.681000 | ...
| 6 | 2019-11-01 14:19:28.681000 | ...
关系的一种方法是:
download has_many download_links
在 downlad.rb 模型中
def download_links
@downloads = Download.includes(:download_links)
render json: {
data:@downloads.map{ |d| d.download_json }, message:"success"
}, status: :ok
end
根据您的命名约定更正名称/关联。