title: Access fields of child classes
body: <p>EDIT : sorry I forgot to mention I was using Mongoid, those <code>field</code> were Mongoid ones</p>
  
  <p>I have a rails application that is responsible for generating Word documents (and replacing some variables inside)</p>
  
  <p>So I have many <code>DocumentEtude::xxxx</code> classes that superclass a base <code>DocumentEtude</code> class (in english <code>ProjectDocument</code>)</p>
  
  <pre><code>class DocumentEtude
    include Mongoid::Document
    field :shared_field
  class DocumentEtude:xxxx &lt; DocumentEtude
    field :only_in_xxxx_field
  </code></pre>
  
  <p>In order to keep my sanity, I'd like to have all the variables in one single place, and do something like this</p>
  
  <pre><code>class DocumentEtude
      ...
      def variables
              vars = {}
              case _type
              when 'DocumentEtude::xxxxx', 'DocumentEtude::yyyyyy', 'DocumentEtude::zzzzzz'
                  vars.merge!({
                      "some_var" =&gt; shared_field,
                      "some_var2" =&gt; only_in_xxxx_field
                      ...
      end
  
      def generate_document
        # Code that uses the variables defined before
      end
  </code></pre>
  
  <p>Now the problem is that I declare this method inside DocumentEtude, however 
  I need to access some fields that are only declared in child classes (<code>only_in_xxxx_field</code> for example), yet apparently Ruby isn't able to find them. Any idea how I could do this ?</p>