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

后端 未结 8 1273
-上瘾入骨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 02:08

    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
    

提交回复
热议问题