Ask A Question

Notifications

You’re not receiving notifications from this thread.

Is this a good fit for storing JSON in database?

Daniel Weaver asked in General

I've got a Video model that represents a video created by the user. During creation of a video a couple of filters with different options can be applied to the video. Currently only 2 but I'll be adding more filters soon.

Would this be a good case for storing the filter options in the database as a single JSON column in the databse (Postgres)?

At first I thought of adding boolean and string/integer columns for each filter (and filter option) but even with just 2 filters this gets pretty verbose:

spectrum: boolean => true/false
spectrum_color: string => white/black
spectrum_width: string => thin/think
spin: boolean => true/false
spin_radius: integer => 200/400/800

Seems much better to store this as a JSON object. Any gotchas I should look out for?

Reply

Seems like a perfect use case to me. You can add / remove options easily in the future without adding more migrations and that'll make maintaining the filter and options a lot easier.

You might look into some of the gems that enforce types on the options if you need to make sure things are cast to booleans, ints, etc.

Reply

Thanks Chris. I found your video on this right after I posted. https://gorails.com/episodes/preferences-settings-with-activerecord-store

Looking at the typed_store gem now 👍 https://github.com/byroot/activerecord-typedstore

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.