ruby 将Rails表列属性设置为只读

pftdvrlh  于 2023-06-22  发布在  Ruby
关注(0)|答案(1)|浏览(83)

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

i1icjdpr

i1icjdpr1#

您似乎在寻找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"

相关问题