# Loading records during class definition

**URL:** <https://www.ruby-forum.com/t/loading-records-during-class-definition/56302>\
**Category:** Rails\
**Created:** [March 30, 2006, 1:04am UTC](https://www.ruby-forum.com/t/loading-records-during-class-definition/56302 "2006-03-30T01:04:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [March 30, 2006, 1:04am UTC](https://www.ruby-forum.com/t/loading-records-during-class-definition/56302/1 "2006-03-30T01:04:13Z")

</div>

I’ve created a look-up table in my database that currently has 4  
rows. I want to access these as constants in the application because  
that’s effectively what they are. I tried this…

class StockRuleLevel \< ActiveRecord::Base  
has\_many :stock\_rules

```
 SITE = find(:first, :conditions => "type_code = 'site'").id
 MAKE = find(:first, :conditions => "type_code = 'make'").id
 MODEL = find(:first, :conditions => "type_code = 'model'").id
 DERIVATIVE = find(:first, :conditions => "type_code =

```

‘derivative’").id  
end

but find() returns nil. Is there a tidy way of doing what I’m trying  
here?

Thanks  
Ashley

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [March 30, 2006, 1:40am UTC](https://www.ruby-forum.com/t/loading-records-during-class-definition/56302/2 "2006-03-30T01:40:34Z")

</div>

Can’t answer the question specifically, but I’d recommend you  
name the constants with \_ID postfix as that’s what they are.

Are you sure you want the ID and not the entire object?

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [March 30, 2006, 2:25am UTC](https://www.ruby-forum.com/t/loading-records-during-class-definition/56302/3 "2006-03-30T02:25:56Z")

</div>

# I tried something similar and had no problem:

# class Folks \< ActiveRecord::Base … TEST = find(:first,:conditions =\> “first\_name = ‘Fred’”).id end

in a controller:  
p Folks::TEST #outputs 45

-John

–  
John S.  
Computing Staff - Webmaster  
Kavli Institute for Theoretical Physics  
University of California, Santa Barbara  
removed\_email\_address@domain.invalid  
(805) 893-6307

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [March 31, 2006, 12:32pm UTC](https://www.ruby-forum.com/t/loading-records-during-class-definition/56302/4 "2006-03-31T12:32:12Z")

</div>

Tom -

Actually I suppose it does make sense to store the whole object

John -

Seeing as you got it working I assumed I was doing something stoopid.  
Turns  
out it’s just the unit tests that break things. Presumably they load  
the  
class before the fixtures are in place (makes sense) so, in the absense  
of a  
simpler way of doing thing, I changed the definition to use methods:

class StockRuleLevel \< ActiveRecord::Base  
has\_many :stock\_rules  
class \<\< self  
%w( site make range model derivative thing ).each do |type\_code|  
eval \<\<-END\_METHOD  
def #{type\_code.upcase}  
StockRuleLevel.find(:first,  
:conditions =\> “type\_code = ‘# {type\_code}’”)  
end  
END\_METHOD  
end  
end  
end

There’s a section in the pickaxe about caching the results of methods  
but I  
doubt these will be hit often enough to matter.

Ashley
