将activerecord关联映射到自定义json格式

时间:2019-05-09 10:17:29

标签: ruby-on-rails activerecord

我有一个activerecord关联

pry(#<Admin::Contents::ContentElementsController>)> content_element.pictures
=> [#<Picture:0x007fb55cb15530
  id: 101,
  name: nil,
  image: "IMG_4833.jpg",
  imageable_id: 95,
  imageable_type: "Content::Element",
  created_at: Thu, 09 May 2019 11:49:13 CEST +02:00,
  updated_at: Thu, 09 May 2019 11:49:13 CEST +02:00,
  caption: nil,
  size: [360, 280],
  position: 1,
  exif: "{\"exif_version\":\"0210\",\"flash_pix_version\":\"0100\",\"color_space\":65535}">,
 #<Picture:0x007fb5625689d8
  id: 105,
  name: nil,
  image: "IMG_4833.jpg",
  imageable_id: 95,
  imageable_type: "Content::Element",
  created_at: Thu, 09 May 2019 12:06:02 CEST +02:00,
  updated_at: Thu, 09 May 2019 12:06:02 CEST +02:00,
  caption: nil,
  size: [360, 280],
  position: 5,
  exif: "{\"exif_version\":\"0210\",\"flash_pix_version\":\"0100\",\"color_space\":65535}">]

它们是通过Carrierwave上传的图片,因此可以调用我需要的网址

content_element.pictures.first.image.path
=> "/Users/xxxx/Projects/test/public/system/staging-uploads/picture/image/101/IMG_4833.jpg"

我的javascript上传插件需要这样的json响应

"file-1":{"url":"/tmp/images/870a7a0bf0fbf4304e89939afdee37ab.jpg","id":"870a7a0bf0fbf4304e89939afdee37ab"},
"file-2":{"url":"/tmp/images/4e819437e9f79889d018179c19a7d833.jpg","id":"4e819437e9f79889d018179c19a7d833"},
"file-3":{"url":"/tmp/images/a3610b99e9f8dcd578572fb8b2285b2f.jpg","id":"a3610b99e9f8dcd578572fb8b2285b2f"}}

我找不到映射我的关联(带有索引)以创建散列的方法oh散列,其中每个散列都具有图片索引之类的名称,而id为id且url为{{1} }

0 个答案:

没有答案
相关问题