method

attr_readonly

attr_readonly(*attributes)
public

Attributes listed as readonly will be used to create a new record but update operations will ignore these fields.

1Note

Warning: prevents persistence but doesn't prevent setting

zubin ยท Sep 9, 20151 thank

For example:

class Widget < ActiveRecord::Base
  attr_readonly :key
end
w = Widget.create! key: 'foo'
w.update! key: 'bar'
w.key #=> 'bar'
w.reload.key #=> 'foo'