Mutuelinvestor
Mutuelinvestor

Reputation: 3538

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. 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. I tried creating a hash of the object and then using to_json, but what resulted did not look like JSON at all.

  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 (i.e Postgres, MySQL, MongoDB).

I appreciate any suggestions, wisdom or advice anyone is will to share.

Upvotes: 1

Views: 1689

Answers (1)

Phlip
Phlip

Reputation: 5343

JSON is a subset of YAML, so start with: 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! 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.

PostgreSQL is a technically superior database. Use MySQL if you are just starting out, because PostgreSQL learned everything it knows about user friendliness from Oracle DDBMS. You can easily serialize to a BLOB in MySQL. But the point of serializing into a PostgreSQL hstore is queries.

A database SELECT statement's WHERE clause cannot always read all of a BLOB or TEXT. Some systems only read the first 256 characters. And the characters will contain JSON/YAML markups and escapes, so you cannot easily query for complex text. 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.

And you probably don't need to query anything in your 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.

Upvotes: 3

Related Questions