Re: Read only model attributes

Jeremy Kemper
Thu, 27 Oct 2005 23:09:39 -0000

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

On Oct 27, 2005, at 3:38 PM, Wiebe Cazemier wrote:
> Jeremy Kemper wrote:
>> Yup!
>> class Foo
>>   def bar=(value)
>>     raise "I''m read-only dammit!"
>>   end
>> end
>>
>
> I''ve got a question about something related. When you override the
> "=" operator like this, is it still necessary to protect against
> mass assignments with "attr_protected"? In other words, does
> assigning like "model.attributes = hash" use the "="
operator, or
> does it generate SQL itself?
>
> I''m pretty sure it is the former, but thought I''d ask,
just to be
> sure.

Yes, attributes= calls each attr_name= method rather than setting the
hash directly.

To match attr_protected behavior, you''d drop the value on the floor
instead of raising an exception:

class Foo
   # Ignore attempts to set bar.
   def bar=(value) end
end

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

iD8DBQFDYV41AQHALep9HFYRAg4+AJsECWMdfjoj077mu92duvPWJ8iv0gCfZs73
au7jTkBcbuYyJT7cabS1nWI=/BUu
-----END PGP SIGNATURE-----