Forum: Ruby on Rails Class Variables in activerecord

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
47ba67a85d1a9a0fd8f8bf8396b0cac0?d=identicon&s=25 Ganesh Gunasegaran (ganeshguna)
on 2007-04-26 18:27
How are class variables persisted using Activerecord?

Cheers,
Ganesh Gunasegaran
6d57db44c5a4e0721cc7f78cf0ce308a?d=identicon&s=25 Stephen Bartholomew (steveb)
on 2007-04-26 18:59
(Received via mailing list)
Hey,

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

Steve
F86901feca747abbb5c6c020362ef2e7?d=identicon&s=25 zdennis (Guest)
on 2007-04-26 20:41
(Received via mailing list)
If you are asking about actual class and class instance variables
here's a brief explanation. If you're talking about variables that
show up in view templates or somewhere else then please be more
descriptive with your question.

Class variables in ruby persist throughout a whole class hierarchy.

   class A
      @@a = 1

      def self.a ; @@a ; end
   end

   A.a # => 1

   class B < A
      @@a = 2
   end

   A.a # => 2
   B.a # => 2


Class instance variables on the other hand exist in the context of a
single class definition.

   class C
      @c = 1
      def self.c ; @c ; end
   end

  C.c # => 1

  class D < C
       @c = 2
  end

  C.c # => 1
  D.c # => 2

Zach

On Apr 26, 12:27 pm, Ganesh Gunasegaran <rails-mailing-l...@andreas-
47ba67a85d1a9a0fd8f8bf8396b0cac0?d=identicon&s=25 Ganesh Gunasegaran (ganeshguna)
on 2007-04-26 20:41
Stephen Bartholomew wrote:
> Hey,
>
> Could you elaborate on the question?  Perhaps post an example of your
> question so we can help you.
>
> Steve

Hmm,

Let us say I have a User class that inherits from ActiveRecord::Base

class User < ActiveRecord::Base
end

The User has two attributes :name and :age

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.column :name, :string
    end
  end

  def self.down
    drop_table :users
  end
end

All the attributes are persisted and are also available thro the
instance variable @attributes.

ex: user = User.create(:name => "gg", :age => 25)
      p user.name #=> "gg"
      p user.attributes["name"] #=> "gg"

So effectively the instance variables are persisted in the model.

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.)

Ignoring error checking "User" model will now look like

class User < ActiveRecord::Base
  @@no_of_users = 0
  def initialize
   @@no_of_users += 1
  end
  def User.no_of_users
    @@no_of_users
  end
end

user1 = User.new
p User.no_of_users # => 1
user2 = User.new
p User.no_of_users # => 2

The problem is, we are not persisting the class variable in the
database, so once we restart the application, the @@no_of_users is reset
back to 0.

My question is how we can persist the class variable @@no_of_users in
the database..

Any thoughts?

Cheers,
Ganesh Gunasegaran.
Fb23bc8cd4030c526b0689276b34c8bd?d=identicon&s=25 Bryan Duxbury (bryanduxbury)
on 2007-04-26 20:47
Maybe dumb question... why not just use count? That'll be up to date. If
you want to avoid doing a lot of queries, then you can query any time
you don't have a cached value, and otherwise just increment the cached
value.
83ca41657a99b65d99889abe712ba5e2?d=identicon&s=25 Jason Roelofs (Guest)
on 2007-04-26 21:01
(Received via mailing list)
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?

Jason
6d57db44c5a4e0721cc7f78cf0ce308a?d=identicon&s=25 Stephen Bartholomew (steveb)
on 2007-04-26 21:13
(Received via mailing list)
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.

Steve
47ba67a85d1a9a0fd8f8bf8396b0cac0?d=identicon&s=25 Ganesh Gunasegaran (ganeshguna)
on 2007-04-26 21:28
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.
83ca41657a99b65d99889abe712ba5e2?d=identicon&s=25 Jason Roelofs (Guest)
on 2007-04-26 22:55
(Received via mailing list)
On 4/26/07, Ganesh Gunasegaran <rails-mailing-list@andreas-s.net> wrote:
> 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.

Jason
8310c5a7c769345114597bcdef111488?d=identicon&s=25 Ben Munat (Guest)
on 2007-04-26 23:16
(Received via mailing list)
Jason Roelofs wrote:
>
> 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
>
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.

b
47ba67a85d1a9a0fd8f8bf8396b0cac0?d=identicon&s=25 Ganesh Gunasegaran (ganeshguna)
on 2007-04-26 23:21
Ben Munat wrote:
> 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.

Well, A class is an object in ruby :P

Getting the point, thanks guys...

Cheers,
Ganesh Gunasegaran.
588ab1c0a5610a7e160a3b101abb91e6?d=identicon&s=25 MichaelLatta (Guest)
on 2007-04-27 01:07
(Received via mailing list)
Ganesh,

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.

Michael


On Apr 26, 2:21 pm, Ganesh Gunasegaran <rails-mailing-l...@andreas-
83ca41657a99b65d99889abe712ba5e2?d=identicon&s=25 Jason Roelofs (Guest)
on 2007-09-25 23:07
(Received via mailing list)
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 user.name #=> "gg"
     p user.attributes["name"] #=> "gg"

User does not have @name and @age. Insead, user.name 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".

Jason
21f7ed21f11a809050594c82eab11d67?d=identicon&s=25 Robert Walker (Guest)
on 2007-09-25 23:09
(Received via mailing list)
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.
153868096910b6bca47e2a9212c2df56?d=identicon&s=25 Frederick Cheung (fcheung)
on 2007-09-26 17:33
Things that are persisted in the database are instances of  subclasses
of ActiveRecord::Base. User is a subclass of ActiveRecord::Base, however
User itself is an instance of Class, not AR:Base, and so can't be
persisted using active record.
If you do want to save that sort of info add a new model for that
purpose.

Fred
This topic is locked and can not be replied to.