0

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!

Dave Collins
  • 1,077
  • 1
  • 15
  • 23
  • I advise you get the whole setup done for 1 intake. Then start doing it for more. You'll find your mistake on your own. – Jatin Ganhotra Aug 25 '12 at 21:20
  • OK Jatin seems like a reasonable plan. Thanks for taking the time to suggest. – Dave Collins Aug 27 '12 at 13:49
  • OK I've got this working for a single intake, and I think I do know what the problem is, but can't find a solution. This gives **two** drop-downs and works, but only the first drop-down's information is passed in the hash: `<%= f.fields_for :intakes do |builder| %> ... <%= select("patient[new_intake_attributes]", "user_id",` Whenever I try to do the recommended `select("patient[new_intake_attributes][]"` I just get **`@patient[new_intake_attributes]' is not allowed as an instance variable name**. _So close yet so far!_ – Dave Collins Sep 11 '12 at 19:50
  • I am unable to make sense out of your comment. Please update your question to reflect your problem and explain the expected behaviour & the result. – Jatin Ganhotra Sep 11 '12 at 20:53
  • OK I've put it all in-line in the question! Thanks again! – Dave Collins Sep 12 '12 at 00:20
  • It's been quite some time I worked on Rails & your problem is quite complex. I suggest you structure your question properly and raise a bounty. – Jatin Ganhotra Sep 12 '12 at 08:22

0 Answers0