我正在使用红宝石CarrierWave来处理我的rails应用程序上的图像,这是一个移动后端API。有没有办法将图像 url 和拇指 url 直接存储在父对象上?
这是默认行为,显示在 post 对象的 json 中。请注意嵌套的 JSON:
{
created_at: "2012-11-17T18:24:04Z",
description: "this is the content",
id: 6,
user_id: 1,
picture: {
url: "/uploads/entry/picture/6/wtf_llama.jpeg",
thumb: {
url: "/uploads/entry/picture/6/thumb_wtf_llama.jpeg"
}
},
updated_at: "2012-11-26T08:16:43Z"
}
我希望看到的:
{
created_at: "2012-11-17T18:24:04Z",
description: "this is the content",
id: 6,
user_id: 1,
picture_url = "/uploads/entry/picture/6/wtf_llama.jpeg",
thumb_url = "/uploads/entry/picture/6/thumb_wtf_llama.jpeg"
updated_at: "2012-11-26T08:16:43Z"
}
这可能吗?
为什么要将这些路径存储在模型中? 改进响应(视图或控制器),而不是持久性层(模型)。 我相信用as_json最容易实现。 添加picture_url
方法来建模或将其他条目合并到最终哈希中。