attr_accessible :field - obj.field="xxx" still possible?!

Hi all

MyClass < ActiveRecord::Base
  attr_protected :field
end

In script/console:

x = MyClass.find 1
# => field = "original"
x.field="blah"
# => "blah"
x.save
# => ...
x = MyClass.find 1
# => field = "original"

Why is there still a field=() method (even though it doesn't work when
saving the object), when I have set it to protected?

Thanks
Josh

Hi all

MyClass < ActiveRecord::Base
attr_protected :field
end

In script/console:

x = MyClass.find 1
# => field = "original"
x.field="blah"
# => "blah"
x.save
# => ...
x = MyClass.find 1
# => field = "original"

Why is there still a field=() method (even though it doesn't work when
saving the object), when I have set it to protected?

because that's not what attr_protected is supposed to do - it is only
supposed to stop the field getting updated when you do
object.update_attributes(...) and similar. why the save isn't working
for you is another issue.

Fred

Joshua Muheim wrote:

because that's not what attr_protected is supposed to do - it is only
supposed to stop the field getting updated when you do
object.update_attributes(...) and similar. why the save isn't working
for you is another issue.

OK, so it is normal, that every field in the database has its own getter
and, method whether it is protected or not, right? Thanks.

Now I just redeclared the field=(str) method to be private. Would you
suggest this to be a clean way to "unset" the method?

Joshua Muheim wrote:
[...]

Now I just redeclared the field=(str) method to be private. Would you
suggest this to be a clean way to "unset" the method?

No. If you want to do that, use remove_method or undef_method.

Best,