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
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 Mornini On Mar 29, 2006, at 2:32 PM, Ashley Moran wrote:> 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
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 Smilanick Computing Staff - Webmaster Kavli Institute for Theoretical Physics University of California, Santa Barbara jsmilani@kitp.ucsb.edu (805) 893-6307 On Mar 29, 2006, at 3:23 PM, Tom Mornini wrote:> 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 Mornini > > On Mar 29, 2006, at 2:32 PM, Ashley Moran wrote: > >> 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 >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060330/90e41c55/attachment.html
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