Rails字段是JSON对象的数组吗? [英] A rails field that is an array of JSON objects?

查看:82
本文介绍了Rails字段是JSON对象的数组吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Damage 表,该表具有以下字段:

I have a Damage table that has the following fields:


  • 说明

  • date_of_damage

我想添加另一个字段来存储损坏点,它们是如下所示的JSON对象:

I'd like to add another field to store Damagepoints, which are JSON objects that look like this:

{ top: 50, left: 100 }

左上角是损坏点的坐标在图表上。用户使用Javascript添加/删除了损坏点

Top and left are the coordinates of the damagepoint on a diagram. The damagepoints are added/removed by the user using Javascript.

但是,我想将其存储在一个字段,是这些损坏点的数组,例如:

However, I'd like to store, in one field, an array of these damagepoints, like this:

@damage.damagepoints = [{left: 40, top: 99}, {left: 100, top: 35}, {left: 150, top: 95}]

我不想使用 Damagepoint 表和 has_many code>关系,因为对此的所有更改都将使用Javascript完成,并且如果用户创建或删除了 Damagepoints ,我只想从客户端更新了 damagepoints 数组,并用新数组替换了数据库中的旧数组。如果使用 has_many 关系,则必须删除所有 Damagepoints 并每次都创建一个新的数组已更新(因为它太复杂了,因为删除/添加特定的损坏点没有好处,因为我不需要跟踪历史记录。)

I don't want to do this using a Damagepoint table with a has_many relationship because all of the changes to this will be done using Javascript, and if Damagepoints are created or removed by the user, I just want to pass from the client the updated array of damagepoints and replace the old one in the database with the new array. If I used a has_many relationship, I'd have to delete all of the Damagepoints and create each one new every time the array was updated (because it is too complicated and there is no benefit from deleting/adding specific damagepoints, as I don't need to track history).

什么是最简单的存储数据的方法,如 @ damage.damagepoints (上面)?我需要做的就是将它(通过控制器)传递给html5数据属性,以便Javascript可以使用它将现有的损伤点添加到图(基于它们的坐标),然后在用户单击保存按钮时通过AJAX调用将更新后的数组(来自html5数据属性)传递回控制器。

What is the easiest way to store data like @damage.damagepoints (above)? All I need to do with it is pass it (via the controller) to an html5 data-attribute so that it can be used by the Javascript to add the existing damagepoints to the diagram (based on their coordinates) and then pass an updated array (from the html5 data-attribute) back to the controller via an AJAX call when the user clicks the 'Save' button.

我正在使用Rails 4.2和Postresql。

I am using Rails 4.2 and Postresql.

谢谢!

推荐答案

在使用postgres时,您很幸运:postgres有一个本机 json 类型。这比使用序列化将数据存储为某种形式的编码字符串更好,因为postgres具有丰富的运算符系列,可让您查询该json数据。

As you're using postgres, you're in luck: postgres has a native json type. This is way better than using serialize to store the data as some form of encoded string, because postgres has a rich family of operators that allow you to query against that json data.

如果您使用的是Postgres 9.4,则还可以使用jsonb类型。通常它会更好,因为它存储了处理后的数据版本(即,它不必不断重复地重新解析数据)并且允许索引。

If you are using postgres 9.4 then you can also use the jsonb type. This is generally better as it stores a processed version of the data (i.e. it doesn't have to keep reparsing the data over and over again) and it allows indexes.

Rails开箱即用地支持此操作(请参见此处),您只需要添加一列类型为json(b)。如果您的迁移包含

Rails supports this out of the box (see here), you just need to add a column of type json(b). If your migration contains

create_table :damages do |t|
  t.string :description
  t.jsonb :damage_points
end

then

Damage.create(damage_points:  [{left: 40, top: 99}, {left: 100, top: 35}])

会创建一个损坏点数据存储为json的行。唯一需要注意的是,尽管您的输入数据的哈希符号中包含符号作为键,但是从数据库中获取数据时,您总是会得到字符串作为键。

would create a row with the damage points data store as json. The only thing to watch out for is that although your input data has symbols as the keys in the hashes, when fetching from the database you'll always get strings back as keys.

这篇关于Rails字段是JSON对象的数组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆