Rails models with immutable attributes

04 Aug 2007

Sometimes you need to make sure that an attribute on one of your models can’t change once it’s set. For example: you’ll want to make it difficult to change @article.permanent_url.

An easy way to do this is to overwrite the setter method (the one that ends in an ‘=’). An even easier way is to install the Immutable Attributes plugin and just do this in your code:

    class BobBarker < Users
      attr_immutable :age, :haircolor

Please if you found this post helpful or have questions.