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.
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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.