szimek
2008-Nov-05 10:25 UTC
Constants in models are initialized before fixtures are loaded?
Hi, here''s simplified code from the model: class Foo < AR::Base FIRST = Foo.first.id end The problem is that when testing I get well known error: "Called id for nil...". I guess that this constant is initialized before the fixtures are loaded. Is there any way to fix it? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Nov-05 10:36 UTC
Re: Constants in models are initialized before fixtures are loaded?
On 5 Nov 2008, at 10:25, szimek wrote:> > Hi, > > here''s simplified code from the model: > class Foo < AR::Base > FIRST = Foo.first.id > end > > The problem is that when testing I get well known error: "Called id > for nil...". I guess that this constant is initialized before the > fixtures are loaded.The constant is initialized when the class is loaded which is indeed before the fixtures are. Does it have to be a constant or would class Foo < ... def self.some_function #cache the value and return it end end be acceptable?> > > Is there any way to fix it? > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---