使Rails表列属性只读

use*_*793 4 ruby-on-rails

使表格列只读的最佳方法是什么?禁用setter方法?该列由postgres触发器设置,因此我不想在应用程序级别设置它

Ily*_*lya 5

看起来你在寻找ActiveRecord :: Base attr_readonly:

class Foo < ActiveRecord::Base
  attr_readonly :bar
end

foo = Foo.create(bar: "first_value")
foo.bar
=> "first_value"
foo.update(bar: "second_value") #column `bar` ignored in SQL query
foo.bar
=> "first_value"
Run Code Online (Sandbox Code Playgroud)