Class Variables in activerecord


Could you elaborate on the question? Perhaps post an example of your question so we can help you.


I get the feeling the OP is coming from the Java / Hibernate realm. In Rails, ActiveRecord objects are not persisted across requests, thus you can’t do what you’re talking about here. And frankly, I’m with Bryan, why aren’t you just using #count?


Sorry, hit “send” to soon.

Also, I need to correct one of your deductions. The attributes on an ActiveRecord object are not instance variables. So your example:

ex: user = User.create(:name => “gg”, :age => 25)

  p []( #=> "gg"
  p user.attributes["name"] #=> "gg"

User does not have @name and @age. Insead, ends up calling #method_missing which ActiveRecord uses to search the @attributes hash for the appropriate key and value. So instance variables are not kept around at all, meaning you can add variables to your model without worrying about ActiveRecord or the database engine complaining about “unknown column”.


I can see why Ganesh is thinking this - class variables will persist over requests if you're using something like mongrel as you'll be running from the same ruby instance. However, on restarting mongrel the ruby environment is reloaded, knocking out the class variables. For this reason, using class would be inaccurate. Most sites will be running on more than one ruby instance meaning that the count will be out of whack.

Class variables are ok for un-changing config vars but not suitable for this task. As suggested, I'd use count.


It sounds like you have a misconception of database persistence. An instance of a model object represents a database table row. Think of the class as the table column headers:

id name password 1 Bill 78d877s8d 2 Steve 8873jdi873837

Now how would you write SQL to store data in the headers (table definition)?

Let us say I need to track the number of users created. So basically I have to maintain the number of users value in the Class level (I understand that we can also store it in a separated model/table, but storing it in the class level makes more sense.)

So from a model-relational mapping system this actually doesn't make since.

Well, the answer is: you don’t. I don’t think a single ORM will let you do this, as it just doesn’t make any sense.


Jason Roelofs wrote:

    Bryan Duxbury wrote:      > Maybe dumb question... why not just use count? That'll be up to date.

    Hi Guys,

    The example I gave was just for illustration.

    As you guys pointed out, we can just use User.count in this example. But     that is not my question here. What if I am crazy and actually wanted to     persist a class variable in the database. Wondering how other ORM's     handle class variables...

    Cheers,     Ganesh Gunasegaran.

Well, the answer is: you don't. I don't think a single ORM will let you do this, as it just doesn't make any sense.


I think what Jason's getting at is this: ORM means *Object* relational mapping... it's about mapping the state of an *object* to a row in the db. A class variable is not part of an object's state, so it doesn't make sense to map into a table holding object data.

If you find yourself needing to persist information about all objects of a given class, them maybe there's a new model there.

However, in the case of counting, yeah that's just information you can observe from the state of the table... i.e. a count query.

If you're concerned about the performance of count queries, rails has an automagic column you can add to your model/table to cache that information.



I think the difference is that in Rails there is not really an attempt to provide a generic ORM solution. Rails is not objects first with a database as an afterthought or just a way to save in-mrmory objects to disk. Mostly those solutions fail anyway (in my experience). The Rails mind-set is that data is stored in a database and the Rails layer does not attempt to hide that or to provide a complete persistence model for everything you can do in Ruby. Rails provides a Ruby layer to access the relational database. The difference in mind- set is a bit subtile but important. Rails does not hide SQL or try to hide the database. It just provides a layer to make that access easier for common cases. The storage model is relational not objects.