Attr_accessor in AR models?

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

You can use attr_accessor in ActiveRecord models. Like this:

class Article < ActiveRecord::Base
attr_accessor :my_prop

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


2010/3/24 elliottg [email protected]

Hi Lasse.

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

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

That clearly explains some of the issues I was having.

Thanks a ton!


Thats because mattr_accessor is at module level, cattr_accessor is at
level, and attr_accessor is at instance level. So you have to use
class.instance_methods to find your method – like this:



2010/3/24 elliottg [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs