OK Updating this question heavily based on progress made, also simplifying this by eliminating some info not pertinent to the problem. I've been reviewing a lot of posts and railscasts about has_many :through but am still having an issue with a relatively simple /new form... Here is the model:
/app/models/user.rb (Think of a user as a Doctor)
has_many :intakes
has_many :patients, :through => :intakes
accepts_nested_attributes_for :intakes
/app/models/intake.rb
belongs_to :user
belongs_to :patient
/app/models/patient.rb
has_many :intakes
has_many :users, :through => :intakes
accepts_nested_attributes_for :intakes
accepts_nested_attributes_for :users
Now, what I want to do is a simple /patients/new and have a form come up with some patient information and two drop-downs for Doctors (users). The classical way to do this has been explained as:
/app/controllers/patients_controller.rb
def new
@patient = Patient.new
2.times { @patient.intakes.build.build_user }
end
and in my view: /app/views/patient/new.html.erb
<%= form_for @patient do |f| %>
<%= render 'fields', :f => f %>
<%= f.submit "Add Patient" %>
<% end %>
And, finally, the fields partial: /app/views/patients/_fields.html.erb
<%= f.fields_for :intakes do |builder| %>
<%= builder.label :first_name, "Cared for by" %>
<%= select("patient[new_intake_attributes]", "user_id",
User.justthishosp(current_user.hospital).collect {
|user|
[ user.first_name+" "+user.last_name, user.id]} ) %>
<% end %>
Now the above actually does cause the form to come up, and there are two "intake" html select elements! Yea! The problems are A) Only the first intake saves because B) The intake HTML format doesn't match what I see in all of the recommendations and C) I cannot determine the proper SELECT syntax to get the HTML format to match the recommendations.
The HTML that the above code produces is:
<label for="patient_intakes_attributes_0_first_name">Cared for by</label>
<select id="patient_new_intake_attributes_user_id"
name="patient[new_intake_attributes][user_id]">
<option value="1"> </option>
<option value="4">Dan Akroyd</option>
<option value="2">Dave Collins</option></select>
</p>
<p>
<label for="patient_intakes_attributes_1_first_name">Cared for by</label>
<select id="patient_new_intake_attributes_user_id"
name="patient[new_intake_attributes][user_id]"><option value="1"> </option>
<option value="4">Dan Akroyd</option>
<option value="2">Dave Collins</option></select>
Note, specifically the form of the select name: name="patient[new_intake_attributes][user_id]"
What they want in Advanced Rails Recipes is: name="patient[new_intake_attributes][][user_id]"
And they way they say you should achieve that is with this select line: select("patient[new_intake_attributes][]", "user_id",
However, that syntax gives me *`@patient[new_intake_attributes]' is not allowed as an instance variable name*
I have tried so many variations of [] and patient, Patient, :patient and I cannot get anything to give me HTML that contains the empty [] after patient[new_intake_attributes]
So, at this point I've got TWO select boxes on the form, but only one saves because only one is being passed in the params hash. Which, BTW looks like this:
(PatientsController.create) params[:patient]:
{"first_name"=>"Nine", "last_name"=>"Niner", ...,
"new_intake_attributes"=>{"user_id"=>"2"}, "pri_loc_id"=>"6"}
and I need:
"new_intake_attributes"=>[{"user_id"=>"2"},{"user_id"=>"4"}]
Or any kind of collection which I could gladly process in my virtual method.
Whew! Holy Smokes! Thanks!