Re: Read only model attributes

Jeremy Kemper
Thu, 27 Oct 2005 21:27:37 -0000

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Oct 27, 2005, at 2:14 PM, Scott Walter wrote:
> Can I somehow setup a Model so that I can prevent a
> column from being updated aka a read only attribute?
> I''m thinking that I need to override the "=" operator
> in my Model class.  Am I on the right track?

Yup!

class Foo
   def bar=(value)
     raise "I''m read-only dammit!"
   end
end

Also see the new :readonly option to find, which makes the entire
record read-only.

begin
   f = Foo.find(:first, :readonly => true)
   f.save
rescue ActiveRecord::ReadOnlyRecord
   puts "Look, but don''t touch!  Thx."
end

Best,
jeremy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDYUZMAQHALep9HFYRAia1AJoDUXeXa6SgN48GAKyvhCD/+qJqjQCgq+N5
7+RXEPZV3Lr59XcCg61+qOY=GF7X
-----END PGP SIGNATURE-----