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

后端 未结 8 1421
-上瘾入骨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:50

    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
    

提交回复
热议问题