-2

I have a multi-tenant application where each tenants information is stores in the database.

I have some variables in the environment configs (development.rb, production.rb) which I want to populate with data from the DB.

Trying to call the model doesn't seem to work.

Is there a way to call a model in the application initializer?

Edit:

Here is the code:

# production.rb
Rails.application.configure do {
  config.custom_variable = MyModel.myattribute
}

This returns a ActiveRecord::ConnectionNotEstablished error

user3188544
  • 1,611
  • 13
  • 21

1 Answers1

1

Initializers run before ActiveRecord is loaded, so you'll need to wait for ActiveRecord before accessing it.

Wrapping your code in an ActiveSupport#on_load block should work.

# production.rb
Rails.application.configure do {
  ActiveSupport.on_load(:active_record) do
     config.custom_variable = MyModel.myattribute
  end
}
fylooi
  • 3,840
  • 14
  • 24