Bradley
2011-Sep-21 19:21 UTC
hooking into `to_prepare` in a Railtie *after* the files have been reloaded (in dev)
I''m working on a gem <https://github.com/bradrobertson/apartment>
that sets
properties on ActiveRecord models (such as table_name) dynamically based on
a user config option.
I have an initializer that achieves this. My problem however is that in dev
mode, these classes are reloaded, so they don''t maintain these values
set.
So I thought I''d use a railtie to hook into the point where these files
are
reloaded and run my config again on the models. My problem however is that
config.to_prepare in the railtie appears to run before the reload! actually
takes place. I can prove this with a bit of logging:
module MyMod
class Railtie < Rails::Railtie
config.to_prepare do
Rails.logger.debug("Contact object_id: #{Contact.object_id}")
end
end
end
if I load up my console, I get the first log:
Contact object_id: 2202692040
If I check Contact.object_id it matches up:
Contact.object_id #=> 2202692040
Then I reload!
reload!
Rails logger from my to_prepare logs:
Contact object_id: 2202692040
So it still has the old object_id, but when I check it in the console:
Contact.object_id #=> 2197355080
Which is the newly loaded class object id.
So how do I get to_prepare to run *after* the files are reloaded? Using Rails
3.0.10
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/sTSf-u8OQ1cJ.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Seemingly Similar Threads
- `method_missing': undefined method `i18n' for #<Rails::Railtie::Configuration:0x000001010fce98>
- Change from 3.0.6 => Rails 3.0.7 "no such file to load -- action_mailer/railtie"
- Model load time
- Railtie initializer and generators
- Problems with database config and prepare_to method
