Last Updated: February 25, 2016
·
632
· descentintomael

Readonly ActiveRecord Columns

ActiveRecord doesn't have a built in means of labeling a column readonly. The best way to work around this is to setup an on update validation like so:

validate :message_cannot_change, on: :update

def message_cannot_change
  if self.message_changed?
    errors.add(:message, "must not change")
  end
end

This will mark the "message" column as readonly. Obviously, you can change that name to whatever column name you would like.