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
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
@@constant_record ||= new(:bibbity => “bobbity”, :boo => “foo”)
[mailto:email@example.com] On Behalf Of Colin L.
Sent: Thursday, May 21, 2009 2:17 AM
Subject: [Rails] Re: Referencing ‘constant’ record in db
2009/5/21 Stephan W. firstname.lastname@example.org
Colin L. wrote:
> Any thoughts anyone?
> 2009/5/20 Colin L. <email@example.com>
I guess here are two, but your question looks vague.
item.id == Item.named_scope_for_constant_record.id
"the item satisfies the condition of the
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
If for the Item class there is just this one special record, can
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.
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.