Forum: Ruby on Rails Object from database record

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.
michau (Guest)
on 2007-07-09 17:20
(Received via mailing list)
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.
Matthew Rudy J. (Guest)
on 2007-07-09 17:37
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.
michau (Guest)
on 2007-07-09 17:51
(Received via mailing list)
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>
Matthew Rudy J. (Guest)
on 2007-07-09 19:35
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>
This topic is locked and can not be replied to.