Forum: Ruby on Rails Constants in models are initialized before fixtures are loaded?

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.
2b2b07fe8688479d2559704d4bf8a264?d=identicon&s=25 Szymon Nowak (szimek)
on 2008-11-05 11:26
(Received via mailing list)
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?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-11-05 11:36
(Received via mailing list)
On 5 Nov 2008, at 10:25, szimek wrote:

> 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?
This topic is locked and can not be replied to.