Object from database record

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.

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.

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. [email protected]

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. [email protected]