如何在不嵌套的情况下从关系向呈现的 JSON 添加属性?

发布于 2024-11-09 08:50:40 字数 1372 浏览 0 评论 0原文

问题是:我正在使用活动记录并返回一些照片对象。这些照片对象的最终消费者将是移动应用程序。

响应需要返回缩略图版本,移动开发人员要求返回的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栖迟 2024-11-16 08:50:40

您可以使用 as_json 方法非常简单地自定义对象的 json 序列化。

为此,您可以这样做:

def as_json(*args)
  hash = super(*args)
  hash.merge!(hash.delete("thumbnails"))
end

或者您可以超级手动完成

def as_json(*args)
  hash = super()
  thumbnails.each do |thumb|
    # build thumbnail json
  end
end

您不必依赖 ActiveRecord 的超级简单的 json 序列化方法。

You can customize the json serialization of an object pretty simply using the as_json method.

For this, you could do :

def as_json(*args)
  hash = super(*args)
  hash.merge!(hash.delete("thumbnails"))
end

Or you could do it super manually

def as_json(*args)
  hash = super()
  thumbnails.each do |thumb|
    # build thumbnail json
  end
end

You don't have to rely on ActiveRecord's super simplistic json serialization methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文