# Attr\_accessor

**URL:** <https://www.ruby-forum.com/t/attr-accessor/119341>\
**Category:** Rails\
**Created:** [November 6, 2007, 8:05am UTC](https://www.ruby-forum.com/t/attr-accessor/119341 "2007-11-06T08:05:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8491ac/32.png) [@pat](https://www.ruby-forum.com/u/pat)\
**Post date:** [November 6, 2007, 8:05am UTC](https://www.ruby-forum.com/t/attr-accessor/119341/1 "2007-11-06T08:05:06Z")

</div>

Folks, newbie here and I have a quick question about attr\_accessor.

I’m developing a very basic CMS and I set up a simple class for a  
user. In that class I use attr\_accessor to set up the read, write,  
etc methods.

Then in a very basic a controller I create an instance variable @post  
= post(:all) and I then render the title of the post along with the  
user name in the view. Strangely, the title appears, but the user  
name doesn’t. BUT, if I remove the attr\_accessor in the model the  
user name appears just fine. I thought that attr\_accessor didn’t do  
that and that methods like attr\_accessible or attr\_protected should be  
used instead to restrict access to the method.

What’s happening here? Do miss understand how attr\_accessor works?

TIA!

---

<div class="post-metadata">

**Author:** ![pat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8491ac/32.png) [@pat](https://www.ruby-forum.com/u/pat)\
**Post date:** [November 6, 2007, 9:56am UTC](https://www.ruby-forum.com/t/attr-accessor/119341/2 "2007-11-06T09:56:11Z")

</div>

On 6 Nov 2007, at 07:04, Pat wrote:

> What’s happening here? Do miss understand how attr\_accessor works?

I suspect the attr\_accessor is overwriting the accessor methods that  
rails creates for your database attributes.  
attr\_protected etc… are only for dealing with mass assignment (ie  
Post.new(params[:post])))

Fred

---

<div class="post-metadata">

**Author:** ![pat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8491ac/32.png) [@pat](https://www.ruby-forum.com/u/pat)\
**Post date:** [November 6, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/attr-accessor/119341/3 "2007-11-06T18:00:31Z")

</div>

On 11/6/07, Pat [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> name doesn’t. BUT, if I remove the attr\_accessor in the model the  
> user name appears just fine. I thought that attr\_accessor didn’t do  
> that and that methods like attr\_accessible or attr\_protected should be  
> used instead to restrict access to the method.

attr\_accessor is a Ruby (not Rails) thing that sets up “getter/setter”  
methods for an instance variable. You shouldn’t use attr\_accessor for  
the columns in an AR model; AR creates getter/setter methods for you.

What are you trying to accomplish with attr\_accessor here?
