I''m working on a nice implementation of lookup tables, which uses ActiveRecord::Base to pull in the values from the tables (only once), provides a nice interface for accessing those values, and ties in directly with the core data tables w/o hitting the db again. The class def for each lookup table has a few non-trivial methods, so repeating them explicitly in .rb files is a no-go. I can''t just extend AR::B, because of STI. And I also can''t figure out how to get it to work as a module mixed-in. So I''m defining my half-dozen or lookup models using eval, in environment.rb. And in production mode this works just fine. But in development mode, my lookup classes are defined only on the first request. Then they go away, and since they''re not defined in the standard way, the auto-require stuff can''t find them again. What are my options to get around this? Is pulling them in via a require_dependency in the application controller the best approach? Is there a good explanation someplace on what "require_dependency" does?
I''m working on a nice implementation of lookup tables, which uses ActiveRecord::Base to pull in the values from the tables (only once), provides a nice interface for accessing those values, and ties in directly with the core data tables w/o hitting the db again. The class def for each lookup table has a few non-trivial methods, so repeating them explicitly in .rb files is a no-go. I can''t just extend AR::B, because of STI. And I also can''t figure out how to get it to work as a module mixed-in. So I''m defining my half-dozen or lookup models using eval, in environment.rb. And in production mode this works just fine. But in development mode, my lookup classes are defined only on the first request. Then they go away, and since they''re not defined in the standard way, the auto-require stuff can''t find them again. What are my options to get around this? Is pulling them in via a require_dependency in the application controller the best approach? Is there a good explanation someplace on what "require_dependency" does?
Michael Schoen wrote:> What are my options to get around this?i) Put your eval''s inside a source file (perhaps in lib) and use require_dependency to load that source file from a place that is run every request. Your app/controllers/application.rb is a good candidate for this. ii) Renounce your slovenly[1] eval-ish ways and use modules. If you are trying to add or call methods on the AR::Base class objects, you will probably want to use Module#included. There is not much that is beyond your grasp with the proper use of included. A pointless example: module AModuleThatNeedsAParentAttr def a_method_that_uses_the_parent_attr parent.do_something end class << self def included(base) base.send :attr_accessor, :parent end end end class X < Object include AModuleThatNeedsAParentAttr end p = Object.new def p.do_something() "I''m a parent!" end x = X.new x.parent = p puts x.a_method_that_uses_the_parent_attr [1] Please don''t be offended by my use of slovenly. It''s just that it''s the word of the day. [2] [2] http://dictionary.reference.com/wordoftheday/
Michael Schoen wrote:> Is pulling them in via a require_dependency in the application > controller the best approach? Is there a good explanation someplace on > what "require_dependency" does?Oops, I didn''t see this line. require_dependency is a mix of require and load. In development mode it acts like load, in production, like require. However, it does not remember the required files and reload them at the beginning of each request. It will only do so if it is called with the file name.
On Monday 29 August 2005 16:08, Michael Schoen wrote:> I''m working on a nice implementation of lookup tables, which uses > ActiveRecord::Base to pull in the values from the tables (only once), > provides a nice interface for accessing those values, and ties in > directly with the core data tables w/o hitting the db again.I think we''ve recently had the discussion under the subject of "Enumerations"; see the list archive. I''ll send you my current implementation in a private message. Michael -- Michael Schuerig Airtight arguments have mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org vacuous conclusions. http://www.schuerig.de/michael/ --A.O. Rorty, Explaining Emotions
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Aug 29, 2005, at 1:30 AM, Michael Schoen wrote:> I''m working on a nice implementation of lookup tables, which uses > ActiveRecord::Base to pull in the values from the tables (only > once), provides a nice interface for accessing those values, and > ties in directly with the core data tables w/o hitting the db again.Have you seen Michael Schuerig''s implementation of enumerations in BoilerPlate? This may be a solved problem (though it is GPL licensed so perhaps not useful.) Best, jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (Darwin) iD8DBQFDFJLDAQHALep9HFYRAi3WAJsFD+zPrGz7BzOL6Jb4Pdek3ZR+9QCg4g8w +ep+D+E4vqjOx0faOEgCjM8=vUGq -----END PGP SIGNATURE-----
My solution''s a bit different, and includes at least 1 interesting feature: One of things I did that I like (and may not apply to others) is to automagically hook up tables to their related enumerations. So if I''ve got a table Dog id name breed_id and a lookup table Breed id name I can do things like access dog.breed, and it checks that while "breed" isn''t a known attribute of a Dog, "breed_id" is, so it tries to get a enumeration class "Breed", looks up the right id, and returns the name field. Really useful for me, not sure if it''s generally applicable. I''m going to clean it up and bit and I''ll post to the list if folks are interested. Jeremy Kemper wrote:> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On Aug 29, 2005, at 1:30 AM, Michael Schoen wrote: > >> I''m working on a nice implementation of lookup tables, which uses >> ActiveRecord::Base to pull in the values from the tables (only once), >> provides a nice interface for accessing those values, and ties in >> directly with the core data tables w/o hitting the db again. > > > Have you seen Michael Schuerig''s implementation of enumerations in > BoilerPlate? > This may be a solved problem (though it is GPL licensed so perhaps not > useful.) > > Best, > jeremy > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.0 (Darwin) > > iD8DBQFDFJLDAQHALep9HFYRAi3WAJsFD+zPrGz7BzOL6Jb4Pdek3ZR+9QCg4g8w > +ep+D+E4vqjOx0faOEgCjM8> =vUGq > -----END PGP SIGNATURE----- > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Tuesday 30 August 2005 19:09, Jeremy Kemper wrote:> On Aug 29, 2005, at 1:30 AM, Michael Schoen wrote: > > I''m working on a nice implementation of lookup tables, which uses > > ActiveRecord::Base to pull in the values from the tables (only > > once), provides a nice interface for accessing those values, and > > ties in directly with the core data tables w/o hitting the db > > again. > > Have you seen Michael Schuerig''s implementation of enumerations in > BoilerPlate? > This may be a solved problem (though it is GPL licensed so perhaps > not useful.)I didn''t notice this originally. BoilerPlate is _L_GPL licensed, because it is meant to be useful (become useful, at any rate). If this licensing is inconvenient for anyone, feel free to contact me about it. Michael -- Michael Schuerig They tell you that the darkness mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org Is a blessing in disguise http://www.schuerig.de/michael/ --Janis Ian, From Me To You