使Rails表列属性只读

时间:2016-05-08 08:18:24

标签: ruby-on-rails

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

1 个答案:

答案 0 :(得分: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"
相关问题