Search code examples
ruby-on-railsactiverecord

Rails attr_readonly doesn't work


According to this question and the documentation of attr_readonly the following should be possible:

class MyModel < ActiveRecord::Base
  attr_accessible :foo
  attr_readonly :bar
end

m = MyModel.create(foo: '123', bar: 'bar') # Should work
m.update_attributes(bar: 'baz')            # Should not work

However the first one fails, saying that I can't mass-assign bar. What am I mising?


Solution

  • From documentation

    attr_accessible takes a list of attributes that will be accessible. All other attributes will be protected.

    So attr_accessible made bar attribute as protected from mass-assignment.