如何在不嵌套的情况下从关系向呈现的 JSON 添加属性?
问题是:我正在使用活动记录并返回一些照片对象。这些照片对象的最终消费者将是移动应用程序。
响应需要返回缩略图版本,移动开发人员要求返回的 JSON 看起来像这样......
{
"root_url":'http://place.s3.amazonaws.com/folder/',
"image_300":'image_300.jpg',
"image_600":'image_600.jpg',
"image_vga":'image_VGA.jpg',
"image_full":'image.jpg'
}
而不是这样:
{
"root_url":'http://place.s3.amazonaws.com/folder/',
"thumbnails": {
"image_300":'image_300.jpg',
"image_600":'image_600.jpg',
"image_vga":'image_VGA.jpg',
"image_full":'image.jpg'
}
}
到目前为止,简单的方法是为每个缩略图创建列,哇,它有效。我不喜欢被锁定,因为如果我们稍后想要不同的缩略图,则意味着将列添加到数据库等。我更愿意只在模型类中指定缩略图,或者有一个单独的缩略图表,其中包含一个桌子每行的拇指。
我查看了委托、composed_of、在连接中使用 GROUP_CONCAT...、使用 :method=>;在 to_json .. 这些看起来都不像选项。有没有简单的方法可以做到这一点?
基本模型示例:
class Photo < ActiveRecord::Base
has_many :thumbnails, :as => :thumbs_for #polymorphic
end
class Thumbnail < ActiveRecord::Base
# columns = name, filename
belongs_to :thumb_for, :polymorphic => true
end
到目前为止,结果如下,基于 jesse reiss 的答案
def as_json(options)
options ||= {} #even if you provide a default, it ends up as nil
hash = super(options.merge({:include => :thumbnails}))
if thumbs = hash.delete(:thumbnails)
thumbs.each {|t| hash.merge!({t['name']=>t['filename']})}
end
hash
end
Here's the problem: I'm using active record and returning some photo objects. The final consumer of these photo objects is going to be a mobile app.
The response needs to have thumbnail versions returned mobile developer has requested that the JSON coming back look like this..
{
"root_url":'http://place.s3.amazonaws.com/folder/',
"image_300":'image_300.jpg',
"image_600":'image_600.jpg',
"image_vga":'image_VGA.jpg',
"image_full":'image.jpg'
}
and not like this:
{
"root_url":'http://place.s3.amazonaws.com/folder/',
"thumbnails": {
"image_300":'image_300.jpg',
"image_600":'image_600.jpg',
"image_vga":'image_VGA.jpg',
"image_full":'image.jpg'
}
}
so far the easy way is to create columns for each of the thumbnails and wow it works. I don't like getting locked into that though because if we wanted different thumbnails later it would mean adding columns to the db etc. I would much prefer to either just specify the thumbnails in the model class OR have a separate table for thumbnails with one thumb per row of the table.
I've looked at delegate, composed_of, using GROUP_CONCAT in a join.., using :method=> in to_json .. none of these look like options. Is there an easy way to do this?
Basic model example:
class Photo < ActiveRecord::Base
has_many :thumbnails, :as => :thumbs_for #polymorphic
end
class Thumbnail < ActiveRecord::Base
# columns = name, filename
belongs_to :thumb_for, :polymorphic => true
end
So far the result looks like this based on the answer from jesse reiss
def as_json(options)
options ||= {} #even if you provide a default, it ends up as nil
hash = super(options.merge({:include => :thumbnails}))
if thumbs = hash.delete(:thumbnails)
thumbs.each {|t| hash.merge!({t['name']=>t['filename']})}
end
hash
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
as_json
方法非常简单地自定义对象的 json 序列化。为此,您可以这样做:
或者您可以超级手动完成
您不必依赖 ActiveRecord 的超级简单的 json 序列化方法。
You can customize the json serialization of an object pretty simply using the
as_json
method.For this, you could do :
Or you could do it super manually
You don't have to rely on ActiveRecord's super simplistic json serialization methods.