12
.
02
.
2025
12
.
02
.
2025
Ruby
Ruby on Rails

Active Record - store vs store_accessor

Michał Łęcicki
Ruby Developer
store-vs-store_accessor by Michał Łęcicki

Imagine this: you get a task to add a Questionnaire feature to the app. It needs to support a variety of questions and must be delivered fast. A JSON column seems perfect for flexible data storage, but how do you integrate it with Rails?

Active Record store

After a quick research, you discover Active Record store method. It adds reader and writer accessors for a defined collection of keys. It also provides dirty tracking and prefix/suffix if needed. Let's try it:

# db/migrate/20250109092657_create_questionnaires.rb

class CreateQuestionnaires < ActiveRecord::Migration[8.0]
  def change
    create_table :questionnaires do |t|
      t.jsonb :questions
      t.string :title

      t.timestamps
    end
  end
end

The model looks like:

# app/models/questionnaire.rb

class Questionnaire < ApplicationRecord
  QUESTION_LIST = %w[question1 question2]

  store :questions, *QUESTION_LIST
end

And you can test it:

require "test_helper"

class QuestionnaireTest < ActiveSupport::TestCase
  test "stores questions in jsonb column" do
    questionnaire = Questionnaire.new
    questionnaire.question1 = "How are you?"
    questionnaire.question2 = "How old are you?"

    questionnaire.save!

    questionnaire.reload
    assert_equal questionnaire.question1, "How are you?"
    assert_equal questionnaire.question2, "How old are you?"
  end
end

Fast and easy, job done. 🎉

But a few weeks after the release, the data team guy comes to you and says: "We need to analyze the questionnaire table, but it contains invalid JSON. Please, fix it."
Sure. 🕵️

You go to the database and see the content of the column. It looks like YAML:

"---\\nquestion1: How are you?\\nquestion2: How old are you?\\n"

Oh! By default, Rails uses the YAML format for data serialization. Let's explicitly add the coder option to the store method (as per documentation). Updating the model should help:

# app/models/questionnaire.rb

class Questionnaire < ApplicationRecord
  QUESTION_LIST = %w[question1 question2]

  store :questions, *QUESTION_LIST, coder: JSON
end

How does the content of the database look now?

"{\\"question1\\":\\"How are you?\\",\\"question2\\":\\"How old are you?\\"}"

It's not a JSON object, it's a JSON string. 🤔
The test is green, which means Rails can serialize it properly, but the content stored in the database is still not a high-quality JSON. We can tell because searching with JSON operators still does not work.

Store_accessor

Confused at this point, let's go back to documentation. There is a NOTE:

NOTE: If you are using structured database data types (e.g. PostgreSQL hstore/json, MySQL 5.7+ json, or SQLite 3.38+ json) there is no need for the serialization provided by .store. Use .store_accessor instead to generate the accessor methods. Be aware that these columns use a string keyed hash and do not allow access using a symbol.

Right! 🤦
You do use structured database type. This means you should not use the store method. Just stick to store_accessor instead:

# app/models/questionnaire.rb

class Questionnaire < ApplicationRecord
  QUESTION_LIST = %w[question1 question2]

  store_accessor :questions, *QUESTION_LIST
end

And test if searching with JSON operators works:

require "test_helper"

class QuestionnaireTest < ActiveSupport::TestCase
  test "stores questions in jsonb column" do
    questionnaire = Questionnaire.new
    questionnaire.question1 = "How are you?"

    questionnaire.save!

    assert_equal questionnaire.reload.question1, "How are you?"
    assert_equal questionnaire.id,
      Questionnaire.where("questions ->> 'question1' = 'How are you?'").first.id
  end
end

Eventually, beautiful JSON resides in the database column:

{
  "question1": "How are you?",
  "question2": "How old are you?"
}

Conclusion

When working with structured data types like JSON or JSONB columns, use the store_accessor method. On the other hand, Active Record store method works fine for older use cases: simple key-value store in plain text columns. If in doubt, read the documentation carefully.

P. S. There is a simple Rails app repo with code examples.

Michał Łęcicki
Ruby Developer

Check my Twitter

Check my Linkedin

Did you like it? 

Sign up To VIsuality newsletter

READ ALSO

Ruby MCP Client in Rails by Paweł Strzałkowski

MCP Client in Rails using ruby-mcp-client gem

11
.
06
.
2025
Paweł Strzałkowski
LLM
Ruby on Rails
Actionmcp in Ruby on Rails by Paweł Strzałkowski

MCP Server with Rails and ActionMCP

11
.
06
.
2025
Paweł Strzałkowski
LLM
Ruby on Rails
Banner - MCP Server with FastMCP and Rails by Paweł Strzałkowski

MCP Server with Rails and FastMCP

11
.
06
.
2025
Paweł Strzałkowski
LLM
Ruby
Ruby on Rails

Ruby on Rails and Model Context Protocol

11
.
06
.
2025
Paweł Strzałkowski
Ruby on Rails
LLM
Title image

Highlights from wroclove.rb 2025

11
.
06
.
2025
Kaja Witek
Conferences
Ruby
Jarosław Kowalewski - Migration from Heroku using Kamal

Migration from Heroku using Kamal

11
.
06
.
2025
Jarosław Kowalewski
Backend
store-vs-store_accessor by Michał Łęcicki

Active Record - store vs store_accessor

11
.
06
.
2025
Michał Łęcicki
Ruby
Ruby on Rails
How to become a Ruby Certified Programmer Title image

How to become a Ruby Certified Programmer

11
.
06
.
2025
Michał Łęcicki
Ruby
Visuality
Vector Search in Ruby - Paweł Strzałkowski

Vector Search in Ruby

11
.
06
.
2025
Paweł Strzałkowski
ChatGPT
Embeddings
Postgresql
Ruby
Ruby on Rails
LLM Embeddings in Ruby - Paweł Strzałkowski

LLM Embeddings in Ruby

11
.
06
.
2025
Paweł Strzałkowski
Ruby
LLM
Embeddings
ChatGPT
Ollama
Handling Errors in Concurrent Ruby, Michał Łęcicki

Handling Errors in Concurrent Ruby

11
.
06
.
2025
Michał Łęcicki
Ruby
Ruby on Rails
Tutorial
Recap of Friendly.rb 2024 conference

Insights and Inspiration from Friendly.rb: A Ruby Conference Recap

11
.
06
.
2025
Kaja Witek
Conferences
Ruby on Rails

Covering indexes - Postgres Stories

11
.
06
.
2025
Jarosław Kowalewski
Ruby on Rails
Postgresql
Backend
Ula Sołogub - SQL Injection in Ruby on Rails

The Deadly Sins in RoR security - SQL Injection

11
.
06
.
2025
Urszula Sołogub
Backend
Ruby on Rails
Software
Michal - Highlights from Ruby Unconf 2024

Highlights from Ruby Unconf 2024

11
.
06
.
2025
Michał Łęcicki
Conferences
Visuality
Cezary Kłos - Optimizing Cloud Infrastructure by $40 000 Annually

Optimizing Cloud Infrastructure by $40 000 Annually

11
.
06
.
2025
Cezary Kłos
Backend
Ruby on Rails

Smooth Concurrent Updates with Hotwire Stimulus

11
.
06
.
2025
Michał Łęcicki
Hotwire
Ruby on Rails
Software
Tutorial

Freelancers vs Software house

11
.
06
.
2025
Michał Krochecki
Visuality
Business

Table partitioning in Rails, part 2 - Postgres Stories

11
.
06
.
2025
Jarosław Kowalewski
Backend
Postgresql
Ruby on Rails

N+1 in Ruby on Rails

11
.
06
.
2025
Katarzyna Melon-Markowska
Ruby on Rails
Ruby
Backend

Turbo Streams and current user

11
.
06
.
2025
Mateusz Bilski
Hotwire
Ruby on Rails
Backend
Frontend

Showing progress of background jobs with Turbo

11
.
06
.
2025
Michał Łęcicki
Ruby on Rails
Ruby
Hotwire
Frontend
Backend