Is there an easy way to make a Rails ActiveRecord model read-only?

Is there an easy way to make a Rails ActiveRecord model read-only?

activerecord readonly attribute
rails read-only database connection
rails 5 readonly
rails has_one readonly
rails activerecord lifecycle
activerecord update
activerecord reload
rails save

I want to be able to create a record in the DB but then prevent Rails from making changes from that point on. I understand changes will still be possible at the DB level.

I believe attr_readonly does what I want on an attribute level, but I don't want to have to manually specify fields... I would rather have more of a white-list approach.

Also, I know there is a :read_only option for associations, but I don't want to limit the "readonlyness" of the object to if it was fetched via an association or not.

Finally, I want to be able to still destroy a record so stuff like :dependent => :destroy works in the associations.

So, to summarize: 1) allow the creation of records, 2) allow the deletion of records, and 3) prevent changing records that have been persisted.


Looking at ActiveRecord::Persistence, everything ends up calling create_or_update behind the scenes.

def create_or_update
  raise ReadOnlyRecord if readonly?
  result = new_record? ? create : update
  result != false
end

So! Just:

def readonly?
  !new_record?
end

Read-Only Models - Today I Learned, You essentially want your model to be read-only until it is time to actually delete it​. This can be Ruby. ActiveRecord 's underlying persistence methods always check readonly? before creating or updating any records. Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core  Is there an easy way to make a Rails ActiveRecord model read-only? (5) I want to be able to create a record in the DB but then prevent Rails from making changes from that point on. I understand changes will still be possible at the DB level.


I've found a more concise solution, which uses the after_initialize callback:

class Post < ActiveRecord::Base
  after_initialize :readonly!
end

readonly? (ActiveRecord::Base), readonly? Importance_1. Ruby on Rails latest stable (v5.2.3) - 1 note - Class: This method is deprecated or moved on the latest stable version. These similar methods exist in v5.2.3: ActiveRecord::Core#readonly? readonly?() public To force an ActiveRecord model to be read only you can do something along these  cs: activerecord: models: category: Kategorie And I need to do this dynamicaly, even when I don't previously know with what model class' instance I will be dealing. So I don't want to explicitly specify "activerecord.models.category". Is there an easy way to do this? I know, that I can do something like this


Why not just create a user on the database that has read only access, and have rails use that account.

However if you want model level access, you can add the following to a specific model:

 def readonly?
    true
  end

  def before_destroy
    raise ActiveRecord::ReadOnlyRecord
  end

Is there an easy way to make a Rails ActiveRecord model read-only?, Is there an easy way to make a Rails ActiveRecord model read-only? activerecord readonly attribute rails 5 readonly rails has_one readonly rails read-​only  In Ruby on Rails, render 'shared/score', :locals => { :score => @foo.score } will not pass in the local? 59 Is there an easy way to make a Rails ActiveRecord model read-only?


This blog post is still valid: http://ariejan.net/2008/08/17/activerecord-read-only-models/

Basically you can rely on ActiveRecord's validation if you add a method:

def readonly?
  true
end

ActiveRecord Read Only Model · devroom.io, Tagged general blog ruby rails ror activerecord as read only all the time. In this example I have a Item model like this: class Item < ActiveRecord::Base end. ActiveRecord::Base provides two methods that may be of interest here: The second, readonly?, return true if the object is read only or false if it isn't. EDIT: Look at the comments and other answers. There are smarter answers than this one! Or try to improve this one as community wiki. Models do not register themselves to a master object, so no, Rails does not have the list of models.


Prevent updates on Rails models (Example), A protip by mbillard about rails, readonly, and read only. Is there an easy way to make a Rails ActiveRecord model read-only? ActiveRecord::  Change manually the Active Record model class into Train. Make migration to change the database table name from cars to trains. Make good search to change references from Car to Train. If you have constantly the need to change database table names, you might want to reconsider naming the tables more abstact way. Like in this case you could have


Implementing read-only models with ActiveRecord – roll no rocks, Implementing read-only models with ActiveRecord data (i.e. data tables that are not created or managed by the Rails app.) The obvious way to do this is at the Model level. The trick to easy testing was to use the ActsAsFu gem to create a test class, and make it ReadOnly after creating test instances:. However, if you follow the conventions adopted by Rails, you'll need to write very little configuration (in some cases no configuration at all) when creating Active Record models. The idea is that if you configure your applications in the very same way most of the time then this should be the default way.


ActiveRecord::Core, Class Public methods …would result in ActiveRecord::Base.configurations to look like this: If you need a copy of your attributes hash, please use the dup method. Duped objects have no id assigned and are treated as new records. with piggy-back attributes will be marked as read only since they cannot be saved. 2 Rails 5.1 run system tests and normal tests with one command Apr 18 '18 1 Configure sidekiq to work without brocker in development environment Apr 27 '17 0 Is there an easy way to make a Rails ActiveRecord model read-only?


Active Record Associations, How to use the methods added to your models by creating associations. Counter cache columns are added to the containing model's list of read-only  I'm looking for a simple way to query updated_at through the associations of an ActiveRecord model. Specifically, I want to be able to query for the records OR related records (belongs_to, has_many)