ruby on rails - Globally accessible class that connects to the db during startup only -


i want create globally accessible class in rails project contain active record result sets also.

@settings = settings.first @locales = locales.all $websitesettings = websitesettings.new(@settings, @locales) 

where should creating class, initializer best place?

i want able access class inside controllers , views.

i understand if data changes in database have restart rails application or update these global instances.

put in initializer , set global constant:

#config/initializers/settings.rb module settings   @settings = settings.first   @locales  = locales.all   website   = websitesettings.new(@settings, @locales) end 

this allow access settings::website throughout app.

this applicable if you're calling data through activerecord.


if weren't accessing data through db, you'd better using 1 of many settings gems, or adding settings directly rails.config class:

#config/application.rb config.settings.title = "test"  #-> rails.configuration.settings.title 

--

as aside, recommend refactor code; you're making 3 db calls series of settings app.

surely put settings inside settings model, scoped around different locales:

#config/initializers/settings.rb module settings   settings = settings.where(locale: :en) end 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -