Rails Activerecord:将jsonb属性读取为字符串

罗伯托·佩扎利

我正在数据库中保存一些图片出口。该列是jsonb数据类型。

为了保存exif,我使用了载波

def store_exif
    if file && model
      begin
        exif = Exif::Data.new(IO.read(file.path))[:exif].to_h
        exif.delete(:maker_note)
        model.exif = exif.to_json
      rescue
      end
    end
  end

当我读取属性时,类型为字符串。

p.exif
=> "{\"exposure_time\":\"1/4\",\"fnumber\":\"8/5\",\"exposure_program\":2,\"iso_speed_ratings\":1250,\"exif_version\":\"0231\",\"date_time_original\":\"2020:10:04 21:42:05\",\"components_configuration\":\"\\u0001\\u0002\\u0003\\u0000\",\"shutter_speed_value\":\"61506/32255\",\"aperture_value\":\"14447/10653\",\"brightness_value\":\"-42954/7669\",\"exposure_bias_value\":\"-15/256\",\"metering_mode\":5,\"flash\":16,\"focal_length\":\"21/5\",\"subject_area\":0,\"color_space\":65535,\"pixel_x_dimension\":4032,\"pixel_y_dimension\":3024,\"sensing_method\":2,\"scene_type\":\"\\u0001\",\"custom_rendered\":8,\"exposure_mode\":0,\"white_balance\":0,\"scene_capture_type\":0,\"flash_pix_version\":\"0100\"}"

p.exif.class
=> String

为什么??我需要带有属性的哈希值...

亚科夫

exif.to_json给你一个字符串。只需将exif保存为哈希值:即可 model.exif = exifRails和Postgres将完成这项工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如果为真,Rails 将字符串合并到现有字符串

在Rails 5中意外地将Jsonb的Postgres数组存储在转义字符串中

Rails ActiveRecord sanitize_sql 替换 ? 在字符串中

如何在Rails的ActiveRecord列上附加字符串

Rails ActiveRecord字符串字段编码与Ruby字符串编码

Rails将RGB颜色字符串显示为色板

将数组从JavaScript解析为Rails上的ruby,但将其作为字符串获取

Rails-如何将数据库中的记录分组/映射为字符串?

Rails方法将空字符串数组[[“”,“”,...])检测为空

Rails操纵字符串

Rails强参数-允许参数为数组或字符串

根据布尔值,Rails使字符串为空白

在Rails中将字符串渲染为变量

如果模型属性为nil,Ruby on Rails返回一个字符串

Rails - 如何将字符串作为查询发送?

将字符串转换为Rails中的类名

Rails/ruby 将字符串转换为 UTC

Rails:将URL字符串转换为参数吗?

Rails - 将变量传递给字符串插值

Rails-将时间转换为字符串

Rails迁移,用于将字符串更改为整数

如何将参数拉入Rails的字符串中

Rails ActiveRecord-通过另一个字符串的子字符串查找

Rails:如何在 Rails 生成的 URL 搜索字符串中将“空格”转义为 %20 而不是 +

将json字符串转换为URL编码的字符串(Rails)

带有字符串和符号的Rails / ActiveRecord订单问题

ActiveRecord固定装置的日期时间字符串格式(非Rails)

Rails + ActiveRecord + Postgres:如何匹配不区分大小写的子字符串?

Ruby on Rails ActiveRecord查询仅选择带有where子句的列字符串字段值?