At the risk of being nitpicky, @@variables are not globals--they are class variables. Globals vars start with a $.
________________________________ From: [email protected] [mailto:[email protected]] On Behalf Of Colin Law Sent: Thursday, May 21, 2009 9:30 AM To: [email protected] Subject: [Rails] Re: Referencing 'constant' record in db 2009/5/21 Stephan Wehner <[email protected]<mailto:[email protected]>> Colin Law wrote: > 2009/5/21 Stephan Wehner > <[email protected]<mailto:[email protected]>> > >> >> > I am trying to avoid a db query each time I reference the (constant) id > when > the application is running, that was the idea of looking it up when the > class is loaded and saving in a constant (which does not work in test > mode > as the fixtures have not always been loaded when the class is loaded) > I meant it is not clear what the nature of this special record is, and what conditions you have. >> >> >> If for the Item class there is just this one special record, can you >> force the id to be simply 0 by manipulating the database? >> > > My initial solution was to have a well-known id for the special record. > The > problem with this in testing is that one has to then include the special > record with that id explicitly in the fixture. One cannot then make use > of > the automatic fixup of habtm tables using the names of the fixture > records > and so have to manually provide fixtures for the habtm join tables. > Plus > the idea of a record in the db having a well-known id is a bit yucky. How about adding this method to your Item class: # is this the special record "named_scope_for_constant_record" ? def special? @@special_record_id ||= Item.named_scope_for_constant_record.id<http://Item.named_scope_for_constant_record.id> self.id<http://self.id> == @@special_record_id_id end Of course, I should have thought of using a global variable rather than a constant. I didn't because I generally consider global variables to be evil. I think this may be the first time for many a year that I have found a good use for one. Though really, though technically it is a global variable, it is being used as a constant. Problem solved, many thanks for helping Colin There is a thread-issue around using ||= ( http://coderrr.wordpress.com/2009/04/29/is-not-thread-safe-neither-is-hashnew-hk ) but that doesn't matter here. Now you can even stub the special? method. Also, if the condition for special? changes you just change the method. Stephan > Colin -- Posted via http://www.ruby-forum.com/. ________________________________ GHC Confidentiality Statement This message and any attached files might contain confidential information protected by federal and state law. The information is intended only for the use of the individual(s) or entities originally named as addressees. The improper disclosure of such information may be subject to civil or criminal penalties. If this message reached you in error, please contact the sender and destroy this message. Disclosing, copying, forwarding, or distributing the information by unauthorized individuals or entities is strictly prohibited by law. --~--~---------~--~----~------------~-------~--~----~ 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 [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---

