0

so I have a tricky issue here I'm not sure how to solve.

  1. I have a Provider model that has_many :educational_affiliations
  2. EducationalAffiliation belongs_to :institution & belongs_to :provider

I have about 9000 universities in my database, so I'm using the handy-dandy rails3-jquery-autocomplete 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.

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)

BUT 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.

Here's what I just tried in my :educational_affiliations, which I assume is the right solution:

class EducationalAffiliation < 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

(i had it working for saving using the attr_accessor, but I've commented it out for now)

So when I render the edit view with the above, I get a ActiveRecord::RecordNotFound: Couldn't find Institution without an ID error — but when I open a debugger on that statement, the model DOES seem to know the institution_id...so confused why it doesn't pick it up.

Am I just doing this in the worst way possible? I assume there's a dumb solution.. :)

Here's the partial that needs the name populated:

.nested-fields
  = f.input :institution_name, url: autocomplete_institution_name_data_path, as: :autocomplete, :input_html => {:id_element => '#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
MBHNYC
  • 1,268
  • 1
  • 13
  • 25

1 Answers1

0

Instead of the virtual attribute method, try the following to define the attribute:

delegate :name, :name=, :to => :institute, :prefix => true 
Tanzeeb Khalili
  • 7,324
  • 2
  • 23
  • 27
  • This is great, I wasn't familiar with the delegate feature yet — one last issue, the name is now populating correctly, but I get a UnknownAttributeError on submit — I could just delete the attribute before the update occurs, but would rather have the model just "get" it.. do you have a suggestion for that fix? – MBHNYC Jul 16 '12 at 16:08
  • Ah! Solved << delegate :name, :name=, :to => :institute, :prefix => true. Delegate setters to the rescue. Yeah, this is too cool to not be documented better. – MBHNYC Jul 16 '12 at 20:41
  • Awesome, I will update the answer to include the setters as well. – Tanzeeb Khalili Jul 17 '12 at 00:11
  • And yeah, definitely a shame it's not better publicized. It was a minor feature that got quietly introduced in Rails 2.2 and never mentioned again... – Tanzeeb Khalili Jul 17 '12 at 00:15