Make AR setter methods private?

Hi,

I have a AR model that I want to limit changes to be only via instance
methods that I’ve added.

How do I prevent my other sw from setting the instance’s attributes?

I know about #attr_protected and #attr_readonly. But the first leaves
the individual setters as they were and the second stops all changes.

I want something like “attr_private”

Thoughts?

Thanks,

Larry

Larry K. wrote:

Hi,

I have a AR model that I want to limit changes to be only via instance
methods that I’ve added.

How do I prevent my other sw from setting the instance’s attributes?

I know about #attr_protected and #attr_readonly. But the first leaves
the individual setters as they were and the second stops all changes.

I want something like “attr_private”

Thoughts?

Thanks,

Larry

In Ruby, you can make any previously defined method private by doing
“private :my_method” in the class. I don’t know if that will break
things in ActiveRecord though.


Josh S.
http://blog.hasmanythrough.com

Hi Josh,

Thanks, I’ll look into that.

Regards,

Larry

On 16 Dec 2007, at 18:39, Josh S. wrote:

In Ruby, you can make any previously defined method private by doing
“private :my_method” in the class. I don’t know if that will break
things in ActiveRecord though.

There’s also the fact that the instance’s setter/getter methods are
only defined later, and so you can’t do
class Customer
private :first_name
end
Since there is no first_name method at that point. The same applies
obviously to making the methods protected. You could probably achieve
something in after_initialize though.

Fred

On Dec 16, 2007 11:16 AM, Frederick C. [email protected]
wrote:

There’s also the fact that the instance’s setter/getter methods are
only defined later, and so you can’t do
class Customer
private :first_name
end
Since there is no first_name method at that point. The same applies
obviously to making the methods protected. You could probably achieve
something in after_initialize though.

Right, but you can certainly define the methods yourself:

class Customer < ActiveRecord::Base
def first_name
read_attribute :first_name
end
private :first_name
end

Pat