Referencing 'constant' record in db

I have a table where one of the records is a constant record that I keep finding myself referencing the id of in code such as
if item.id == Item.named_scope_for_constant_record.id
where the named scope involves a db query.

I thought it would be a good idea to use a constant for this, so in the Item model:
class Item<ActiveRecord::Base
CONSTANT_RECORD_ID = named_scope_for_constant_record.id

end

Then I can reference Item::CONSTANT_RECORD_ID which is determined when the class is loaded.

This works fine in my application, but in my tests it appears the class is loaded before the fixtures as the find fails. It is ok if I run ‘ruby test/unit/mytest.rb’ but not if I ‘rake test’.

Is my analysis of what is happening correct? If so is there a better way of achieving the desired effect?

Colin

Any thoughts anyone?

Colin

Colin Law wrote:

Any thoughts anyone?

Colin

I guess here are two, but your question looks vague.

Does

item.id == Item.named_scope_for_constant_record.id

not become

  "the item satisfies the condition of the
named_scope_for_constant_record" ?

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?

Stephan

Colin Law wrote:

Any thoughts anyone?

Colin

I guess here are two, but your question looks vague.

Does

item.id == Item.named_scope_for_constant_record.id

not become

"the item satisfies the condition of the

named_scope_for_constant_record" ?

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)

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.

Colin

First, consider whether it's worth worrying about this--it may be that rails' and/or the db's caching mechanisms make this not such a perf issue.

That said, would it be feasible to not store this record in the db at all? Just make the constant a free-standing AR object? Something like:

class MyModel < AR::Base
  def MyModel.constant_record
    @@constant_record ||= new(:bibbity => "bobbity", :boo => "foo")
  end
end

HTH,

-Roy

Colin Law wrote:

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

At the risk of being nitpicky, @@variables are not globals–they are class variables. Globals vars start with a $.

At the risk of being nitpicky, @@variables are not globals–they are class variables. Globals vars start with a $.

Yes of course, I am still getting the hang of ruby. I think my comments still apply, though not quite as strongly. I suppose if I wrap it in a class method I can probably live with my conscience.

Of course my whole concept of having a ‘special’ record is a bit yucky. I think maybe some refactoring is in order to improve the whole design.

Colin

Colin Law wrote:

Yes of course, I am still getting the hang of ruby. I think my comments
still apply, though not quite as strongly. I suppose if I wrap it in a
class method I can probably live with my conscience.

Not necessarily. I see nothing wrong with using class variables. In Java
they are used all the time and are called "Fields" otherwise known as
static variable. Java actually doesn't have actual constants. A
"constant" in Java would be a static final variable. Declaring it static
makes it a variable on the class and final makes it immutable (unable to
change once initialized).

I think you're still confusing class variable and globals. Globals break
encapsulation, class variables do not.