Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3.2.x has_many, through (3 models) calls nil id before create
    text
    copied!<p>Here is what I am dealing with, but first a bit of background on what needs to be done. There are 3 models: Patients -- Appointments -- Procedures</p> <p>Within these 3 models there are two views Procedures -- Patients</p> <p>Of these 2 views I would like to schedule Appointments through the Patients (show) view. Which in essence will create a new appointment for the patient in view (specifically the patient.id in view). </p> <p>Here is the code for the models</p> <pre class="lang-rb prettyprint-override"><code>class Patient &lt; ActiveRecord::Base attr_accessible :address1, :address2, :city, :comment, :email, :first_name, :init_date, :init_time, :last_name, :mobile, :notes, :phone, :state, :zip has_many :appointments, dependent: :destroy has_many :procedures, through: :appointments </code></pre> <pre class="lang-rb prettyprint-override"><code>class Procedure &lt; ActiveRecord::Base attr_accessible :comment, :occurence, :procedure, :procedure_code, :procedure_price, :procedure_time, :visits has_many :appointments has_many :patients, through: :appointments </code></pre> <pre class="lang-rb prettyprint-override"><code>class Appointment &lt; ActiveRecord::Base attr_accessible :appointment_date, :appointment_notes, :appointment_time, :procedure_id belongs_to :patient belongs_to :procedure </code></pre> <p>Here is the controller for appointments as well as the routes.rb inclusion of appointments (but just that line)</p> <pre class="lang-rb prettyprint-override"><code>resources :appointments, only: [:create, :destroy, :edit, :update] </code></pre> <pre class="lang-rb prettyprint-override"><code>class AppointmentsController &lt; ApplicationController include PatientsHelper before_filter :signed_in_user def create @current_patient = @patient.id @appointment = @current_patient.appointments.build(params[:appointment]) if @appointment.save flash[:success] = "Appointment scheduled!" redirect_to patient_path(@current_patient) else render 'create' end end </code></pre> <pre class="lang-rb prettyprint-override"><code>module PatientsHelper def current_patient=(patient) @current_patient = patient end def current_patient @current_patient end def current_patient?(patient) patient == current_patient end end </code></pre> <p>So that is the setup, I am receiving the following rspec error in the patients_controller. Have tested @appointment = @current_patient.appointments.build(params[:appointment]) in console by first defining an id for @current_patient = Patient.first. That works and a build happens as it should. The error:</p> <p>Failures:</p> <pre><code>1) Appointment Pages appointment creation with invalid information should not create an appointment Failure/Error: expect { click_button "Schedule procedure" }.not_to change(Appointment, RuntimeError: Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id # ./app/controllers/appointments_controller.rb:6:in `create' # (eval):2:in `click_button' # ./spec/requests/appointment_pages_spec.rb:17:in `block (5 levels) in &lt;top (required)&gt;' # ./spec/requests/appointment_pages_spec.rb:17:in `block (4 levels) in &lt;top (required)&gt;' 2) Appointment Pages appointment creation with invalid information error messages Failure/Error: before { click_button "Schedule procedure" } RuntimeError: Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id # ./app/controllers/appointments_controller.rb:6:in `create' # (eval):2:in `click_button' # ./spec/requests/appointment_pages_spec.rb:22:in `block (5 levels) in &lt;top (required)&gt;' 3) Appointment Pages appointment creation with valid information should create a micropost Failure/Error: expect { click_button "Schedule procedure" }.to change(Appointment, RuntimeError: Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id # ./app/controllers/appointments_controller.rb:6:in `create' # (eval):2:in `click_button' # ./spec/requests/appointment_pages_spec.rb:36:in `block (5 levels) in &lt;top (required)&gt;' # ./spec/requests/appointment_pages_spec.rb:36:in `block (4 levels) in &lt;top (required)&gt;' </code></pre> <p>It would seem that I am not properly defining @current_patient = @patient.id... or rather the current patient in view is not carrying over/through to the appointments form. Where do I need to define the current patient to carry that id through to the form create method of @current_patient.appointments.build? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload