r/rails • u/onyx_blade • May 22 '26
Exploring an API idea: per-file write permissions for ActiveRecord models
To support such an API:
include ActiveRecordReadOnly
end
# Anywhere else in the codebase — blocked
Post.find(1).update!(title: "x")
# => ActiveRecord::ReadOnlyRecord
# In a service class — allowed
include Post::Writable
post.update!(published: true) # works
end
end
It's possible with a hack (checking caller_locations on every model readonly? call). Repo has the details, including why it's not production-ready and why refinements (my first thought) didn't work:
https://github.com/onyxblade/active_record_read_only
Curious what people think of this pattern.
15
Upvotes
2
u/scalarbanana May 22 '26
Maybe you can also do this with refinements? It’s an interesting idea
2
u/Valashe May 22 '26
Take a look at tagged logger's api.
Instead of per-file it seems like you could do within a certain block. You'd have permissions fields on the model instance or the model class, you'd set those permissions when you call the block and roll them back afterwards in an ensure guard, and you'd yield. Maybe a thread local instead. Don't need to do all of the runtime checking of callers which seems.... slow, among other things.
To get your per-file you could write a helper metaprogramming method to wrap calls by the method defined hook. Or you could just have a
allow_writes_to User, in: :my_method.That said, I don't really see the vision here. Anyone could pretty easily write arbitrary sql and have it update the models. It seems almost impossible to get the actual security right here. Maybe could be useful, when modified, as a test suite check. For ex, checking every "subscription update" method only touches subscriptions, and not the plans or products.
4
u/MeanYesterday7012 May 22 '26
Just cause you can doesn’t mean you should. Stop trying to make rails something it isn’t. Easier to maintain and onboard engineers if you stick to rails conventions.
1
u/mowkdizz May 22 '26
I like the general idea. How does it work with inheritance?
2
u/paneq May 23 '26
We did something similar in our 1.5M lines Ruby project. Only the engine which owns ActiveRecord model could write to it.
It worked an was beneficial in enforcing that other modules/engines need to use explicit surface (service objects) exposed by the engine which owns the model.
In other words, it gives you a nice boundary protection.
TLDR:
Foo::Model can be updated by Foo::Service, but Baz::Service can't update Foo::Model directly, only by invoking Foo::Service.
2
u/ultrathink-art May 23 '26
Relevant use case this pattern handles: coding agents that have Rails access. Free-form write access through the model layer means an agent can modify records during exploration or summarization — not intentional, just how they work. Scoping writes to explicit service objects lets you safely expose reads without picking up unintended mutations.
1
u/dg_ash May 24 '26
Just make sure you check the indexes
1
u/No_Ostrich_3664 May 24 '26
When I’m thinking off read only data. I’m lean towards keeping it in memory if possible. Yml, constants etc…
1
u/Rafert May 22 '26
We’ve done something like this by enabling write behaviour using Thread.current in a couple of service classes, and a Rubocop rule preventing that being set outside of an allowlist of files.
4
u/[deleted] May 22 '26
[removed] — view removed comment