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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Rudy <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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. > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Rudy <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---