# Accessing model class variables in a controller

**URL:** <https://www.ruby-forum.com/t/accessing-model-class-variables-in-a-controller/68196>\
**Category:** Rails\
**Created:** [September 10, 2006, 7:10am UTC](https://www.ruby-forum.com/t/accessing-model-class-variables-in-a-controller/68196 "2006-09-10T07:10:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [September 10, 2006, 7:10am UTC](https://www.ruby-forum.com/t/accessing-model-class-variables-in-a-controller/68196/1 "2006-09-10T07:10:53Z")

</div>

Hello,

In my model Example, I’ve defined three types as such:

class Example \< ActiveRecord::Base  
@@TYPE\_A = ‘A’  
@@TYPE\_B = ‘B’  
@@TYPE\_C = ‘C’

def Example.select…  
end  
end

How can I access these types in my controller. I’m trying the following  
with my controller named Read:

def exm  
a = Example.TYPE\_A  
end

It won’t allow me to declare the types and gives me a syntax error. Any  
help would be appreciated. Aren’t these just class variable  
declarations and should be available through the class?

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [September 10, 2006, 11:12am UTC](https://www.ruby-forum.com/t/accessing-model-class-variables-in-a-controller/68196/2 "2006-09-10T11:12:25Z")

</div>

On Sep 9, 2006, at 10:10 PM, Sam D. wrote:

> Hello,

Hello.

> How can I access these types in my controller. I’m trying the  
> following  
> with my controller named Read:
> 
> def exm  
> a = Example.TYPE\_A  
> end

That’s not how class variables work. I haven’t read it, but word on  
the street is `Ruby for Rails’ by David A. Black is a good resource  
for learning Ruby with Rails. The Pickaxe is fantastic also (google:  
Ruby Pickaxe).

> It won’t allow me to declare the types and gives me a syntax  
> error. Any  
> help would be appreciated. Aren’t these just class variable  
> declarations and should be available through the class?

Yes. What you want is cattr\_accessor. It works just like  
attr\_accessor, but for class variables.

Class variables can be tricky. Check out this nutty blog post to  
understand their nuances: [Sessions N Such — err.the\_blog](http://errtheblog.com/post/22)

–  
Chris W.

> **[err.the\_blog](http://errtheblog.com)**
>
> Err the Blog: Ruby and Rails from the front.

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [September 10, 2006, 11:28am UTC](https://www.ruby-forum.com/t/accessing-model-class-variables-in-a-controller/68196/3 "2006-09-10T11:28:26Z")

</div>

Hi –

On Sun, 10 Sep 2006, Sam D. wrote:

> def Example.select…  
> It won’t allow me to declare the types and gives me a syntax error. Any  
> help would be appreciated. Aren’t these just class variable  
> declarations and should be available through the class?

You’re confusing class variables with constants, and constants with  
methods 🙂

Class variables (@@var) are variables with a scope that includes

1. the class in whose scope they were created
2. all descendants of that class
3. all instances of any of those classes

What class variables _don’t_ have is any implication for methods; that  
is, when you assign to a class variable, you’re just assigning to a  
variable, not creating any methods that either get or set the value of  
that variable.

If you want such methods, you have to write them:

class C  
def self.cvar\_x # access the cvar from the class  
@@x  
end

```
 def cvar_x # access it from instances of the class
   @@x
 end

```

end

or create/use a facility for doing so, like the “cattr\_\*” methods that  
are used internally in Rails.

By the way, the somewhat strange behavior of “class variables” – in  
particular, that they’re not actually class-specific, but are more  
class-hierarchy-specific – is due to change in future versions of  
Ruby. Then there are people like me, who think class variables cause  
(and will continue to cause, even with that change) much more trouble  
than they’re worth, and wish they would go away entirely. But I don’t  
think Matz is in that camp 🙂

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [September 10, 2006, 12:06pm UTC](https://www.ruby-forum.com/t/accessing-model-class-variables-in-a-controller/68196/4 "2006-09-10T12:06:30Z")

</div>

On Sep 10, 2006, at 2:45 AM, removed\_email\_address@domain.invalid wrote:

> I’ve just added a response to that post, which will save some space  
> here 🙂

Great points and great comment. Thanks for the insight!

–  
Chris W.

> **[err.the\_blog](http://errtheblog.com)**
>
> Err the Blog: Ruby and Rails from the front.

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [September 10, 2006, 11:56am UTC](https://www.ruby-forum.com/t/accessing-model-class-variables-in-a-controller/68196/5 "2006-09-10T11:56:29Z")

</div>

Hi –

On Sun, 10 Sep 2006, Chris W. wrote:

> > def exm  
> > a = Example.TYPE\_A  
> > end
> 
> That’s not how class variables work. I haven’t read it, but word on  
> the street is `Ruby for Rails’ by David A. Black is a good resource  
> for learning Ruby with Rails. The Pickaxe is fantastic also (google:  
> Ruby Pickaxe).

I’ve responded in detail to Sam’s question because my book, though  
I’m happy to see it recommended, says very little about class  
variables. I like to think it’s not just because I dislike them, but  
also because I really haven’t seen very many reasons to use them.  
People learning Rails will definitely want to know them when they see  
them, but beyond that I didn’t have much to say about them.

> > It won’t allow me to declare the types and gives me a syntax  
> > error. Any  
> > help would be appreciated. Aren’t these just class variable  
> > declarations and should be available through the class?
> 
> Yes. What you want is cattr\_accessor. It works just like  
> attr\_accessor, but for class variables.

It’s misnamed, though, because it implies that class variables can  
store object “attribute” values. Since they cut across many objects,  
they’re not really representing any object’s attributes.

> Class variables can be tricky. Check out this nutty blog post to  
> understand their nuances: [Sessions N Such — err.the\_blog](http://errtheblog.com/post/22)

I’ve just added a response to that post, which will save some space  
here 🙂

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [September 10, 2006, 1:52pm UTC](https://www.ruby-forum.com/t/accessing-model-class-variables-in-a-controller/68196/6 "2006-09-10T13:52:40Z")

</div>

All of this makes this clear. I did want to mention that my use case  
for  
these class variables was not to actually write to them, but to just use  
them to define a set of possible values for a particular attribute in an  
object. In other words, they are sort of used as a constant, but within  
the  
scope of that class. I didn’t want to declare these outside of my  
model class as constants b/c there’s no reason why they should be  
available  
elsewhere.

If this design decision does not sound good, I’d like to know why.

Thanks.
