I have the following code:
Cms::Page.class_eval do
def self.fetch_by_slug(slug)
Rails.cache.fetch("cms_page_#{slug}") { Cms::Page.find_by_slug(slug) }
end
end
in config/initializers/cms_mp.rb
The thing is, I have a call to fetch_by_slug in my layout file, and thus, it’s called for each request. In development mode, if I do something that triggers a reload of the classes, I receive the error message undefined method fetch_by_slug for #<Class:0xbff20e8>, and the only fix is to restart the rails devel server.
Why is this happening? How can I fix it?
It looks like the problem is that initializers are loaded only on server bootup. I your Cms::Page is reloaded latter then the added method i gone. You should consider to put this code not in initializer but somwere where it will be reloaded with dependent classes.