Accessing model class variables in a controller

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?

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


Chris W.

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 :slight_smile:

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 :slight_smile:

David


David A. Black | [email protected]
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 | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

On Sep 10, 2006, at 2:45 AM, [email protected] wrote:

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

Great points and great comment. Thanks for the insight!


Chris W.

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

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

David


David A. Black | [email protected]
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 | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

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.