Learn how to use StoreModel Gem in Ruby On Rails.

Bhawsartanmay
2 min readMay 16, 2021

StoreModel is basically a Rails Gem through which we can wrap JSON-backed DB columns in a similar way we work with ActiveRecord models.

Let’s understand this gem with the help of an example:

For instance, imagine that you have a model Computer with a jsonb column called configuration.

And Computer can have many types of models, and each model can have different sets of configurations. So, the usual workflow probably looks like this:

computer = Computer.find(params[:id])
if computer.configuration["model"] == "Dell-N92xyz"
computer.configuration["processor"] = "i5"
computer.configuration["ram"] = "4GB"
end
computer.save

The above approach works fine when you don’t have a lot of keys with validation logic around them and just read the data. However, when you work with that data more intensively (for eg, adding some validations around it as per the Computer model) — you may find the code a bit verbose and error-prone. With the StoreModel gem, the snipped above could be rewritten this way:

computer = Computer.find(params[:id])
if computer.configuration.model == "Dell-N92xyz"
computer.configuration.processor = "i5"
computer.configuration.ram = "4GB"
end
computer.save

Let’s look into the installation process:

  1. Add the below line in your gemfile:
gem 'store_model'

2. Then run the bundle command:

$ bundle install

Or, you can run directly this command:

$ gem install store_model

Let see, How to use StoreModel:

Let’s add the attributeconfiguration in Computer Model.

class Computer < ApplicationRecord
attribute :configuration, Configuration.to_type
end

Now, define the configuration attributes like this:

class Configuration
include StoreModel::Model
attribute :model, :string
attribute :processor, :string
attribute :ram, :string
end

That’s it, Now we use a JSON-backed column as an AR.

Now, let’s define validations for the configuration JSON column:

class Configuration
include StoreModel::Model
attribute :model, :string
attribute :processor, :string
attribute :ram, :string
validates :processor, presence: true
end

Now, we need to add validation in the Computer model to make the above validation work:

class Computer < ApplicationRecord
attribute :configuration, Configuration.to_type

validates :configuration, store_model: true
end

That’s it, I hope you enjoyed this blog. If yes, then please give me some claps.
You can reach me out at bhawsartanmay@gmail.com .

My Linkedin profile:linkedin.com/in/tanmay-bhavsar-ruby-and-golang-developer-39a61966

Happy coding and see you soon!

Took Reference from this: https://github.com/DmitryTsepelev/store_model

Thank You
Tanmay Bhawsar

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Bhawsartanmay
Bhawsartanmay

No responses yet

Write a response