67760

Rails: Run initializer before creating classes

Basically I have an initializer class at RAILS_ROOT/config/initialiers/app_constant.rb to make everything easy to control.

class AppConstant APIURL = 'http://path.to.api' end

And in the RAILS_ROOT/model/user.rb, I have the settings:

class User < ActiveResource::Base self.site = AppConstant::APIURL end

And when run rails s, I got the following error

<class:User>: uninitialized constant User::AppConstant::APIURL

I know that the problem is because Rails run Initializers after creating the Classes. Is there any way to make some Initializers run before Rails setup it classes?

<hr>

Finally this problem is solved by adding require "#{Rails.root}\conf\initializers\app_constant.rb" to the application.rb which is loaded right before Rails loads models.

Answer1:

To have code run before Rails itself is loaded, put it above require 'rails/all' in config/application.rb.

Answer2:

Another solution would be to wrap the constant in a method so it is not evaluated when the class is loaded, but only later when the method is called:

def self.site AppConstant::APIURL end

If it needs to be settable as well:

def self.site=(url) @site = url end def self.site @site ||= AppConstant::APIURL end

Recommend

  • i want to use a REST api, i cannot manage to set active resource to use it
  • Why isn't bundle install allowing for the pg gem to fully install?
  • “Failed create pod sandbox” pod error in AWS Kubernetes cluster
  • Heroku rake task uninitialized constant for MongoMapper model
  • Heroku installing sqlite3 even though it is not a dependecy
  • Will volatile protect me from all behind-the-scenes multi-threading hazards?
  • Array resizing and realloc function
  • Show message when Tform2 is created?
  • In matplotlib, how do you change the fontsize of a single figure?
  • Spring: No transaction manager has been configured
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • pillow imaging ImportError
  • accepts_nested_attributes_for practical form use for in Rails 3
  • Object and struct member access and address offset calculation
  • Visual Studio 2010 debugger build correctly - compiler pdb and linker pdb not in synch?
  • How to get Eclipse Oxygen to run on Java 9
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • All Classes Conforming to Protocol Inherit Default Implementation
  • script to move all files from one location to another location
  • WinForms: two way TextBox problem
  • Can I make an Android app that runs a web view in Chrome 39?
  • Websockets service method fails during R startup
  • XCode can't find symbols for a specific iOS library/framework project
  • Turn off referential integrity in Derby? is it possible?
  • Authorize attributes not working in MVC 4
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF