# ROR Dynamic Class

**URL:** <https://www.ruby-forum.com/t/ror-dynamic-class/260387>\
**Category:** Uncategorized\
**Created:** [October 18, 2020, 8:11pm UTC](https://www.ruby-forum.com/t/ror-dynamic-class/260387 "2020-10-18T20:11:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![C3po](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ba8739/32.png) [@C3po](https://www.ruby-forum.com/u/C3po)\
**Post date:** [October 18, 2020, 8:11pm UTC](https://www.ruby-forum.com/t/ror-dynamic-class/260387/1 "2020-10-18T20:11:00Z")

</div>

Hello,  
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}")
                         ElasticSearch.const_set("Repository#{year}",
                                    Class.new {

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

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

```

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!
