title: Supplying data for a form re-population from a different model
body: <p>so I have a tricky issue here I'm not sure how to solve.</p>
  
  <ol>
  <li>I have a Provider model that has_many :educational_affiliations</li>
  <li>EducationalAffiliation belongs_to :institution &amp; belongs_to
  :provider</li>
  </ol>
  
  <p>I have about 9000 universities in my database, so I'm using the handy-dandy <a href="https://github.com/crowdint/rails3-jquery-autocomplete" rel="nofollow">rails3-jquery-autocomplete</a> gem to give me type-ahead support.  That's all working great - on TOP of that I'm using cocoon to support the nesting of the :educational_affiliations form inside of the provider's edit form.</p>
  
  <p>So here's where the issue comes — This works great for submitting new affiliation records, (I'm using some jquery to set the :institution_id on the :educational_affiliations_attributes object, works great)</p>
  
  <p><strong>BUT</strong> when I return to edit this later, of course the :institution_name isn't populated, because it's not part of the :educational_affiliations model, it's in the :institution model.</p>
  
  <p>Here's what I just tried in my :educational_affiliations, which I assume is the right solution:</p>
  
  <pre><code>class EducationalAffiliation &lt; ActiveRecord::Base
    attr_accessible :degree, :graduation_year, :honors, :institution_name, :institution_id, :provider_id
    belongs_to :institution
    belongs_to :provider
  
    # attr_accessor :institution_name
  
    validates :institution_id, :graduation_year, :provider_id, :degree, presence: true
  
    def institution_name
      Institution.find(institution_id).name
    end
  
  end
  </code></pre>
  
  <p>(i had it working for saving using the attr_accessor, but I've commented it out for now)</p>
  
  <p>So when I render the edit view with the above, I get a <strong>ActiveRecord::RecordNotFound: Couldn't find Institution without an ID</strong> error — <strong>but when I open a debugger on that statement, the model DOES seem to know the institution_id</strong>...so confused why it doesn't pick it up.</p>
  
  <p>Am I just doing this in the worst way possible? I assume there's a dumb solution.. :)</p>
  
  <p>Here's the partial that needs the name populated:</p>
  
  <pre><code>.nested-fields
    = f.input :institution_name, url: autocomplete_institution_name_data_path, as: :autocomplete, :input_html =&gt; {:id_element =&gt; '#provider_educational_affiliations_institution_id'}
    = f.input :institution_id, as: :hidden
    = f.input :provider_id, as: :hidden, input_html: { value: current_provider.id }
    = link_to_remove_association "Remove Degree", f
  </code></pre>