Create Accessors for JSONB column data in Rails
I was recently reading about the ActiveRecord::Store API and just today, I got the chance to use it. It's a really great API for working with serialized data in Rails.
Let's consider the following model Geolocation
, with a jsonb
column data
. The contents of the data
column look something like this -
{
"coordinates": {
"latitude": "5.887692972524717",
"longitude": "-162.08234016158394"
}
}
The store_accessor
creates getters and settings for coordinates
.
class Geolocation < ApplicationRecord
store_accessor :data, :coordinates
end
Geolocation.last.coordinates
# => {"latitude" => "5.887692972524717", "longitude" => "-162.08234016158394"}
In my case, I have nested data that I'd like to create accessors for - latitude
and longitude
. From what I could find, this API doesn't support nested data yet, so we have to bring in a helper from ActiveModel::Attributes. We declare the coordinates
portion as a jsonb attribute.
class Geolocation < ApplicationRecord
store_accessor :data, :coordinates
store_accessor :coordinates, :latitude, :longitude
attribute :coordinates, :jsonb
end
geo = Geolocation.new
geo.coordinates
# => nil
geo.latitude
# => nil
geo.longitude
# => nil
geo.latitude = 5.887692972524717
# => 5.887692972524717
geo.coordinates
# => {"latitude"=>5.887692972524717}
geo.longitude = -162.08234016158394
# => -162.08234016158394
geo.coordinates
# => {"latitude"=>5.887692972524717, "longitude"=>-162.08234016158394}
I_Ran_Out_Of_Words_Hopefully_You_Get_My_Point :)
S/O Vlad & LayeredDesignForRailsApplications
https://devdocs.io/rails~7.1/activerecord/store
Tweet