title: Rails association for two foreign keys for the same table in one table
body: <p>I am new to RoR and still playing with associations. I need to have two references to a particular model in another model. The scaffolded code doesn't work and I get a "uninitialized constant" error.</p>
  
  <p>Generation commands:</p>
  
  <pre><code>script/generate scaffold BaseModel name:string
  script/generate scaffold NewModel name:string base1:references base2:references
  db:migrate
  </code></pre>
  
  <p>Generated models:</p>
  
  <pre><code>class NewModel &lt; ActiveRecord::Base
       belongs_to :base1
       belongs_to :base2
  end
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class BaseModel &lt; ActiveRecord::Base
       has_many :new_models # I added this line
  end
  </code></pre>
  
  <p>When I try to create a new_model at <code>/new_models/new</code>, I tried both the ID and the name of the BaseModel but it does not work. The error I get is:</p>
  
  <pre><code>uninitialized constant NewModel::Base1
  </code></pre>
  
  <p>I guessed it maps the names, so in my create method, I tried to explicitly set the BaseModel instances:</p>
  
  <pre><code>@new_model = NewModel.new(params[:new_model])
  @base1 = BaseModel.find(1) # this exists
  @base2 = BaseModel.find(2) # this exists
  @new_model.base1 = @base1  # This throws the same error as above
  </code></pre>
  
  <p>Is there anything I'm missing?</p>