# Object from database record

**URL:** <https://www.ruby-forum.com/t/object-from-database-record/108918>\
**Category:** Rails\
**Created:** [July 9, 2007, 3:20pm UTC](https://www.ruby-forum.com/t/object-from-database-record/108918 "2007-07-09T15:20:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![michau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@michau](https://www.ruby-forum.com/u/michau)\
**Post date:** [July 9, 2007, 3:20pm UTC](https://www.ruby-forum.com/t/object-from-database-record/108918/1 "2007-07-09T15:20:25Z")

</div>

I have a problem with creating objects from database records.  
In the model Room I have declared instance variable (at the beginning  
of a class)

@exits = {:south=\>nil, :north=\>nil}

The database has been already populated with rooms.

Now, if somewhere in a program i use Room.find(1), I get the object  
room, however it doesn’t see the @exists hash. How can I make it see  
it?  
I’m totally new to RoR and this being probably straightforward problem  
just drives me maaad. Help please.

---

<div class="post-metadata">

**Author:** ![michau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@michau](https://www.ruby-forum.com/u/michau)\
**Post date:** [July 9, 2007, 3:37pm UTC](https://www.ruby-forum.com/t/object-from-database-record/108918/2 "2007-07-09T15:37:57Z")

</div>

I imagine your code looks like this;

class Klass \< ActiveRecord::Base  
attr\_reader :exits

@exits = {:south =\> nil, :north =\> nil}  
end

this will set an instance variable, but for the class.  
this will work nicely.

class Klass \< ActiveRecord::Base  
DEFAULT\_EXITS = {:south =\> nil, :north =\> nil}  
def exits  
@exits ||= DEFAULT\_EXITS.dup  
end  
attr\_writer :exits  
end

Is that what you want?

michau wrote:

> I have a problem with creating objects from database records.  
> In the model Room I have declared instance variable (at the beginning  
> of a class)
> 
> @exits = {:south=\>nil, :north=\>nil}
> 
> The database has been already populated with rooms.
> 
> Now, if somewhere in a program i use Room.find(1), I get the object  
> room, however it doesn’t see the @exists hash. How can I make it see  
> it?  
> I’m totally new to RoR and this being probably straightforward problem  
> just drives me maaad. Help please.

---

<div class="post-metadata">

**Author:** ![michau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@michau](https://www.ruby-forum.com/u/michau)\
**Post date:** [July 9, 2007, 3:51pm UTC](https://www.ruby-forum.com/t/object-from-database-record/108918/3 "2007-07-09T15:51:43Z")

</div>

I think not… Basically I want to have an instance variable, that is  
not related to any column from the database mapped to the object. And  
I want to be able to see that instance variable when creating the  
object through ModelName.find(id).

On Jul 9, 2:37 pm, Matthew R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![michau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@michau](https://www.ruby-forum.com/u/michau)\
**Post date:** [July 9, 2007, 5:35pm UTC](https://www.ruby-forum.com/t/object-from-database-record/108918/4 "2007-07-09T17:35:06Z")

</div>

ok,  
well, I suggest you try the code I gave you.  
but it won’t set the variable until you try to call it.

If you really want it to be set when you find, then i think this’ll  
work,  
but it’s hacky.

class Klass \< ActiveRecord::Base  
DEFAULT\_EXITS = {:south =\> nil, :north =\> nil}  
attr\_accessor :exits

def initialize(\*args)  
@exits = DEFAULT\_EXITS.dup  
super(\*args)  
end  
end

michau wrote:

> I think not… Basically I want to have an instance variable, that is  
> not related to any column from the database mapped to the object. And  
> I want to be able to see that instance variable when creating the  
> object through ModelName.find(id).
> 
> On Jul 9, 2:37 pm, Matthew R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
