# Testing Readonly Models

DevFeed: [Testing Readonly Models](<https://devfeed.tech/articles/testing-readonly-models-20120.md>)

Original publisher: [Read original article](<https://hashrocket.com/blog/posts/testing-readonly-models>)

Author: Tony Yunker

Published: 2026-03-17T13:00:00Z

Content type: tutorial

Language: en

Sources: [Hashrocket](<https://devfeed.tech/sources/hashrocket.md>)

Topics: [Rails](<https://devfeed.tech/topics/rails.md>), [Testing](<https://devfeed.tech/topics/testing.md>), [Ruby](<https://devfeed.tech/topics/ruby.md>), [Databases](<https://devfeed.tech/topics/databases.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [activerecord](<https://devfeed.tech/tags/activerecord.md>), [code](<https://devfeed.tech/tags/code.md>), [database](<https://devfeed.tech/tags/database.md>), [rails](<https://devfeed.tech/tags/rails.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [ruby-on-rails](<https://devfeed.tech/tags/ruby-on-rails.md>), [testing](<https://devfeed.tech/tags/testing.md>)

## AI overview

A Rails tutorial explains how to test readonly models that normally reject database writes. It proposes a narrowly scoped test-only override that temporarily permits persistence during setup and restores readonly behavior before the test runs.

## Source excerpt

I was working with a readonly model in Rails the other day and ran into an issue whilst testing it. Here's what I ran into and the solution I came up with. Readonly models are a great way to signal that, well, you should only ever read them, not write them. Maybe you have some external system that connects to the database for writes, or maybe your Rails app connects to some data warehouse for some queries or reports. It's can be useful to have a safeguard to prevent accidental errant writes. It's actually really simple to make a model readonly, you just need to override the readonly? method: class ReadOnlyPost < ApplicationRecord def readonly? = true end Now any attempt to create/save/update/delete a ReadOnlyPost will raise a friendly ActiveRecord::ReadOnlyRecord exception. The Problem You might be able to see where this is going. For any tests that can avoid saving this readonly model to the database, (i.e. using ReadOnlyPost.new or FactoryBot.build), then we're all good. But often tests need to persist some records to the test database. And if I try to create a ReadOnlyPost, I'm going to have a bad time. RSpec.describe ReadOnlyPost, type: :model do let(:post) { ReadOnlyPost.create(title: "Title", body: "body") } it "can create a post" do expect(post).to be_a(ReadOnlyPost) end end % bundle exec rspec F Failures: 1) ReadOnlyPost can create a post Failure/Error: let(:post) { ReadOnlyPost.create(title: "Title", body: "body") } ActiveRecord::ReadOnlyRecord: ReadOnlyPost is marked as readonly # ./spec/models/read_only_post_spec.rb:4:in `block (2 levels) in <top (required)>' # ./spec/models/read_only_post_spec.rb:7:in `block (2 levels) in <top (required)>' This makes sense, the readonly property of the model doesn't go away in test - creating a record is creating a record is writing to the database, so it will fail. The Solution What we want to do is override this constraint to temporarily allow us to persist data. Ideally, we do this very narrowly to not impact the syst