# Attr\_accessor in AR models?

**URL:** <https://www.ruby-forum.com/t/attr-accessor-in-ar-models/185708>\
**Category:** Rails\
**Created:** [March 24, 2010, 9:21pm UTC](https://www.ruby-forum.com/t/attr-accessor-in-ar-models/185708 "2010-03-24T21:21:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [March 24, 2010, 9:21pm UTC](https://www.ruby-forum.com/t/attr-accessor-in-ar-models/185708/1 "2010-03-24T21:21:57Z")

</div>

Am I correct in thinking that one cannot use attr\_accessor in  
ActiveRecord models?

I have success when using either cattr\_accessor or mattr\_accessor but  
not the native Ruby method. Am I missing something here, or is this  
how it really is?

Thanks. Elliott G

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [March 24, 2010, 9:30pm UTC](https://www.ruby-forum.com/t/attr-accessor-in-ar-models/185708/2 "2010-03-24T21:30:08Z")

</div>

You _can_ use attr\_accessor in ActiveRecord models. Like this:

class Article \< ActiveRecord::Base  
attr\_accessor :my\_prop  
end

Maybe you could post some code and/or error message.

/Lasse

2010/3/24 elliottg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [March 24, 2010, 10:17pm UTC](https://www.ruby-forum.com/t/attr-accessor-in-ar-models/185708/3 "2010-03-24T22:17:33Z")

</div>

Hi Lasse.

I think I may be able to get to the point a bit better with 2 more  
questions…

Am I correct in thinking that attr\_accessor always returns or sets an  
instance variable?

Another reason I’m confused, is that, when I set an attr\_accessor :foo  
in my model and then drop into script/console and call .methods on  
that model instance, I never see “foo” or “foo=” in the methods list  
when I run a search for them. Conversely when setting either  
cattr\_accessor :foo or mattr\_accessor :foo shows the the two new  
“foo” and “foo=” methods in the model’s method list as expected.

What am I missing here?

Thanks, Elliott G

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [March 24, 2010, 10:46pm UTC](https://www.ruby-forum.com/t/attr-accessor-in-ar-models/185708/4 "2010-03-24T22:46:14Z")

</div>

That clearly explains some of the issues I was having.

Thanks a ton!

EG

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [March 24, 2010, 10:37pm UTC](https://www.ruby-forum.com/t/attr-accessor-in-ar-models/185708/5 "2010-03-24T22:37:27Z")

</div>

Thats because mattr\_accessor is at module level, cattr\_accessor is at  
class  
level, and attr\_accessor is at instance level. So you have to use  
class.instance\_methods to find your method â€“ like this:

class\_name.instance\_methods.grep(/method\_name/)

/Lasse

2010/3/24 elliottg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
