Colin Law wrote:
I am trying to avoid a db query each time I reference the (constant) id
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
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.
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
the automatic fixup of habtm tables using the names of the fixture
and so have to manually provide fixtures for the habtm join tables.
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” ?
@@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
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