Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Rails table column attribute read only

What is the best way to make my table column read only? disable the setter method? The column is set by postgres trigger, so I don't want to set it in the application level

like image 950
user1883793 Avatar asked May 08 '16 08:05

user1883793


1 Answers

It seems like you look for 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"
like image 196
Ilya Avatar answered Oct 16 '22 01:10

Ilya