简体   繁体   English

我应该在我的Rails应用程序的活动记录中存储JSON

[英]Should I Store JSON In active record for my Rails Application

I'm working on a horse racing application in ruby on rails. 我正在铁轨上的红宝石上进行赛马应用。 In a nut shell, I receive text files of horse racing charts and import them into my application. 在坚果壳中,我收到赛马图表的文本文件并将其导入我的应用程序。 The application seeks to provide the user an easy way to review the racing charts and to also provide various statistical insights and filtering. 该应用程序旨在为用户提供一种简单的方法来查看比赛图表,并提供各种统计见解和过滤。 I've developed a pretty descent data model for the numerous fields of data that should address the statical and filtering aspects of the application. 我已经为众多数据领域开发了一个漂亮的下降数据模型,它应该解决应用程序的静态和过滤方面问题。 However, when it comes to allowing the users to easily review the charts I'm not happy with my solution. 但是,当允许用户轻松查看图表时,我对我的解决方案不满意。 Currently I have to take all the various fields of data and create a chart object. 目前,我必须采取所有各种数据领域并创建图表对象。 This seems very inefficient to me. 这对我来说似乎效率很低。 Alternatively, I could create a JSON chart object at the time of import and then store the serialized object in active record. 或者,我可以在导入时创建JSON图表对象,然后将序列化对象存储在活动记录中。 Typically users will be requesting multiple chart objects at a time so speed is important. 通常,用户将一次请求多个图表对象,因此速度很重要。 Here's a Link TO Sample Chart . 这是一个链接到样本图表 As you can see, it's a pretty complex data structure. 如您所见,这是一个非常复杂的数据结构。

I've only been at rail development a couple years and its only a hobby at this point so I was hoping to get some input from some more experienced rails developers. 我只是在铁路开发方面工作了几年而且这只是一个业余爱好,所以我希望得到一些更有经验的铁路开发人员的一些意见。 Here are a couple of questions: 以下是几个问题:

  1. Is what I'm suggesting a common approach to problems such as mine or is there a better way of handling this. 我是建议采用一种常见的方法解决我的问题,还是有更好的方法来解决这个问题。

  2. Assuming the approach has merit, what's the best/right way to create the JSON object. 假设该方法具有优点,那么创建JSON对象的最佳/正确方法是什么。 I tried creating a hash of the object and then using to_json, but what resulted did not look like JSON at all. 我尝试创建对象的哈希,然后使用to_json,但结果看起来根本不像JSON。

  3. Are there any Gems that would make this task easier. 是否有任何宝石可以使这项任务更容易。

  4. If I intend to pursue this course does it make sense to use on datastore over another (ie Postgres, MySQL, MongoDB). 如果我打算继续这个课程,那么在数据存储上使用它是否合理(即Postgres,MySQL,MongoDB)。

I appreciate any suggestions, wisdom or advice anyone is will to share. 我感谢任何人愿意分享的任何建议,智慧或建议。

JSON is a subset of YAML , so start with: https://www.google.com/search?q=rails+store+yaml+in+blob JSON是YAML的子集,因此请从以下网址开始: https//www.google.com/search? q=rails+store+yaml+in+blob

A database string of arbitrary length is either a TEXT or a BLOB, and the latter is easier to google for! 任意长度的数据库字符串是TEXT或BLOB,后者更容易谷歌! If you put your text field into ActiveRecord::Base.serialize , Rails will automatically encode a hash as YAML going into the database, and decode it back to a hash coming out. 如果将文本字段放入ActiveRecord::Base.serialize ,Rails会自动将散列编码为YAML进入数据库,并将其解码回哈希值。

PostgreSQL is a technically superior database. PostgreSQL是一个技术上优越的数据库。 Use MySQL if you are just starting out, because PostgreSQL learned everything it knows about user friendliness from Oracle DDBMS. 如果刚刚开始使用MySQL,因为PostgreSQL从Oracle DDBMS中学到了所有关于用户友好性的知识。 You can easily serialize to a BLOB in MySQL. 您可以轻松地在MySQL中序列化为BLOB。 But the point of serializing into a PostgreSQL hstore is queries. 但序列化到PostgreSQL hstore的重点是查询。

A database SELECT statement's WHERE clause cannot always read all of a BLOB or TEXT. 数据库SELECT语句的WHERE子句不能总是读取所有BLOB或TEXT。 Some systems only read the first 256 characters. 有些系统只读取前256个字符。 And the characters will contain JSON/YAML markups and escapes, so you cannot easily query for complex text. 并且字符将包含JSON / YAML标记和转义,因此您无法轻松查询复杂文本。 But PostgreSQL SELECT statements are aware that hstore fields contain JSON, and they support a special notation to query them by their keys and values. 但是PostgreSQL SELECT语句知道hstore字段包含JSON,并且它们支持一种特殊的符号来通过它们的键和值来查询它们。

And you probably don't need to query anything in your JSON. 而且您可能不需要查询JSON中的任何内容。 This brings me to my final point: Even if you are just doing a learner project, write lots of automated tests. 这让我想到了最后一点:即使您只是在做一个学习者项目,也要编写大量自动化测试。 None of these decisions are set in stone, so write whatever works now, keep it clean, keep the tests passing, and when the time comes to upgrade (such as a migration from MySQL to PostgreSQL), the tests will help you. 这些决定都不是一成不变的,所以写下现在正常工作,保持干净,保持测试通过,以及何时升级(例如从MySQL迁移到PostgreSQL),测试将帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM