i have "immutable" class, , needs stay way logging/accounting purposes.
how ensure no 1 updates columns on model? have seen https://stackoverflow.com/a/14781183/902907 , it's great solution column-by-column basis, want every column.
i'd avoid read_only_attributes because fail quietly.
how about:
class journalentry < activerecord::base before_update { fail activerecord::readonlyrecord } end
this allow new journal entries created, if attempt save, fail.
j = journalentry.new j.amount = "21.85" j.save # => true j.amount = "39.00" j.save # => activerecord::readonlyrecord
alternatively, approach utilizes readonly!
method provided activerecord:
class journalentry < activerecord::base after_initialize { readonly! if persisted? } after_create { readonly! } end
however, both techniques have shortcoming: not prevent mutating database using low-level calls. example, update_column
, bypasses validations , callbacks, defeat these read-only checks.