# Can non-ActiveRecord variables be temporarily stored in a class?

**URL:** <https://www.ruby-forum.com/t/can-non-activerecord-variables-be-temporarily-stored-in-a-class/174261>\
**Category:** Rails\
**Created:** [August 23, 2009, 11:54pm UTC](https://www.ruby-forum.com/t/can-non-activerecord-variables-be-temporarily-stored-in-a-class/174261 "2009-08-23T23:54:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![coolaj86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@coolaj86](https://www.ruby-forum.com/u/coolaj86)\
**Post date:** [August 23, 2009, 11:54pm UTC](https://www.ruby-forum.com/t/can-non-activerecord-variables-be-temporarily-stored-in-a-class/174261/1 "2009-08-23T23:54:09Z")

</div>

I’m creating a PDF directory using PDF::Writer.

The directory has a few pieces.

- The photo directory is sorted by group
- The appendix is sorted by last name

I want the appendix to show the page number on which the person is  
printed, but I don’t want to store that in the database.

I would like to do this:  
contact.page\_number = page\_number  
contacts \<\< contact

but currently I’m doing this  
tuple[:contact =\> contact, :page\_number =\> page\_number]  
contacts \<\< tuple

---

<div class="post-metadata">

**Author:** ![coolaj86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@coolaj86](https://www.ruby-forum.com/u/coolaj86)\
**Post date:** [August 24, 2009, 9:52am UTC](https://www.ruby-forum.com/t/can-non-activerecord-variables-be-temporarily-stored-in-a-class/174261/2 "2009-08-24T09:52:22Z")

</div>

On Aug 23, 10:41 pm, CoolAJ86 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m creating a PDF directory using PDF::Writer.
> 
> The directory has a few pieces.
> 
> - The photo directory is sorted by group
> - The appendix is sorted by last name
> 
> I want the appendix to show the page number on which the person is  
> printed, but I don’t want to store that in the database.

An activerecord object is a normal ruby object - it can have non  
database store instance variables if you want.

Fred

---

<div class="post-metadata">

**Author:** ![coolaj86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@coolaj86](https://www.ruby-forum.com/u/coolaj86)\
**Post date:** [August 25, 2009, 7:41pm UTC](https://www.ruby-forum.com/t/can-non-activerecord-variables-be-temporarily-stored-in-a-class/174261/3 "2009-08-25T19:41:36Z")

</div>

> > The directory has a few pieces.
> > 
> > - The photo directory is sorted by group
> > - The appendix is sorted by last name
> 
> > I want the appendix to show the page number on which the person is  
> > printed, but I don’t want to store that in the database.
> 
> An activerecord object is a normal ruby object - it can have non  
> database store instance variables if you want.

but when I do this:  
contact.page\_number = page\_number  
where page\_number doesn’t exist in db/migrate/001\_create\_contacts.rb  
or app/model/contacts.rb  
I get this:  
undefined method `page\_number=’ for #Contact:0xb6bbb5b4

---

<div class="post-metadata">

**Author:** ![coolaj86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@coolaj86](https://www.ruby-forum.com/u/coolaj86)\
**Post date:** [August 25, 2009, 8:09pm UTC](https://www.ruby-forum.com/t/can-non-activerecord-variables-be-temporarily-stored-in-a-class/174261/4 "2009-08-25T20:09:41Z")

</div>

On Aug 25, 6:38 pm, CoolAJ86 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> but when I do this:  
> contact.page\_number = page\_number

Well yes - you still need to write the accessor methods to store you  
instance variables (attr\_accessor is probably enough).

Fred

---

<div class="post-metadata">

**Author:** ![coolaj86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@coolaj86](https://www.ruby-forum.com/u/coolaj86)\
**Post date:** [August 30, 2009, 9:38am UTC](https://www.ruby-forum.com/t/can-non-activerecord-variables-be-temporarily-stored-in-a-class/174261/5 "2009-08-30T09:38:11Z")

</div>

> Well yes - you still need to write the accessor methods to store you  
> instance variables (attr\_accessor is probably enough).
> 
> Fred

That makes sense. Thanks.

I had my JavaScript thinking cap on when I was doing this… thinking  
to create accessors out of thin air.

I also just found out that the virtual accessors are accessors, not  
class variables.  
return @updated\_at # always null  
return updated\_at # works

---

<div class="post-metadata">

**Author:** ![coolaj86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@coolaj86](https://www.ruby-forum.com/u/coolaj86)\
**Post date:** [August 30, 2009, 10:19am UTC](https://www.ruby-forum.com/t/can-non-activerecord-variables-be-temporarily-stored-in-a-class/174261/6 "2009-08-30T10:19:10Z")

</div>

2009/8/30 CoolAJ86 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I also just found out that the virtual accessors are accessors, not  
> class variables.  
> return @updated\_at # always null  
> return updated\_at # works

Don’t understand this.  
As I understand it if you have  
attr\_accessor :my\_var  
then @my\_var will access the variable (but this may only be written  
inside the class)  
and my\_var is a method (well two methods actually) that may be used  
externally to read/write to @my\_var so you can say  
my\_object.my\_var = 1  
x = my\_object.my\_var

If you use my\_var (no @) inside the class this should work but it is  
calling the accessor methods rather than directly accessing @my\_var

Colin

---

<div class="post-metadata">

**Author:** ![coolaj86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@coolaj86](https://www.ruby-forum.com/u/coolaj86)\
**Post date:** [August 30, 2009, 1:46pm UTC](https://www.ruby-forum.com/t/can-non-activerecord-variables-be-temporarily-stored-in-a-class/174261/7 "2009-08-30T13:46:57Z")

</div>

2009/8/30 Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > > Fred
> > 
> > Don’t understand this.
> 
> I think what the previous poster has realised is that activerecord  
> attributes are not stored inside individual instance variables.

Ah, yes

Colin

---

<div class="post-metadata">

**Author:** ![coolaj86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@coolaj86](https://www.ruby-forum.com/u/coolaj86)\
**Post date:** [August 30, 2009, 3:25pm UTC](https://www.ruby-forum.com/t/can-non-activerecord-variables-be-temporarily-stored-in-a-class/174261/8 "2009-08-30T15:25:18Z")

</div>

Maybe this is useful

[http://forums.site5.com/showthread.php?t=18522](http://forums.site5.com/showthread.php?t=18522)

---

<div class="post-metadata">

**Author:** ![coolaj86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@coolaj86](https://www.ruby-forum.com/u/coolaj86)\
**Post date:** [August 30, 2009, 12:48pm UTC](https://www.ruby-forum.com/t/can-non-activerecord-variables-be-temporarily-stored-in-a-class/174261/9 "2009-08-30T12:48:26Z")

</div>

On 30 Aug 2009, at 09:17, Colin L. wrote:

> > I had my JavaScript thinking cap on when I was doing this… thinking  
> > to create accessors out of thin air.
> > 
> > I also just found out that the virtual accessors are accessors, not  
> > class variables.  
> > return @updated\_at # always null  
> > return updated\_at # works
> 
> Don’t understand this.

I think what the previous poster has realised is that activerecord  
attributes are not stored inside individual instance variables.

Fred
