# Make AR setter methods private?

**URL:** <https://www.ruby-forum.com/t/make-ar-setter-methods-private/123342>\
**Category:** Rails\
**Created:** [December 16, 2007, 7:28pm UTC](https://www.ruby-forum.com/t/make-ar-setter-methods-private/123342 "2007-12-16T19:28:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [December 16, 2007, 7:28pm UTC](https://www.ruby-forum.com/t/make-ar-setter-methods-private/123342/1 "2007-12-16T19:28:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [December 16, 2007, 7:39pm UTC](https://www.ruby-forum.com/t/make-ar-setter-methods-private/123342/2 "2007-12-16T19:39:35Z")

</div>

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](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [December 16, 2007, 8:13pm UTC](https://www.ruby-forum.com/t/make-ar-setter-methods-private/123342/3 "2007-12-16T20:13:29Z")

</div>

Hi Josh,

Thanks, I’ll look into that.

Regards,

Larry

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [December 16, 2007, 8:16pm UTC](https://www.ruby-forum.com/t/make-ar-setter-methods-private/123342/4 "2007-12-16T20:16:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [December 16, 2007, 9:51pm UTC](https://www.ruby-forum.com/t/make-ar-setter-methods-private/123342/5 "2007-12-16T21:51:56Z")

</div>

On Dec 16, 2007 11:16 AM, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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
