ROR Dynamic Class

For scaling reason I had to slipt an ElastiSearch index in many indexes basicaly by year. So to request it in a simple manner I ceated a Repository class for each year and since I am writing in ruby, I decided to generate my classes at runtime this way:

 module ElasticSearch
    require 'elasticsearch/persistence'

  module Repository 
           def self.init_repositories
                 [2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019].each do |year|
                  unless Object.const_defined?("ElasticSearch::Repository#{year}")

                                      include Elasticsearch::Persistence::Repository
                                      include Elasticsearch::Persistence::Repository::DSL

                                      @@es_client = Elasticsearch::Model.client
                                      @@indexname = "#{year}_elastic_repository_#{Rails.env}"
                                      @@indextype = @@indexname

Things are working nicely at least in production and my classes are nicely found but in dev mode it is a pain… I have often errors telling me that my classes are not existing. I added my init_repositories in initializers and even call it direclty before the error but it does not work… Does any one has a idea for me?

Tks a lot!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs