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

后端 未结 8 1271
-上瘾入骨i
-上瘾入骨i 2021-01-31 01:25

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.

8条回答
  •  北恋
    北恋 (楼主)
    2021-01-31 01:59

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

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

提交回复
热议问题