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
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COwhat do you want to do with this line `@current_patient = @patient.id`? You set an ID number for `@current_patient`, now `@current_patient` is a number, so you could not call `appointments` method. Also, I think you need to render new action when `@appointment` was not saved, not create action.
      singulars
    2. COBecause this is going through the Patients view pages I want to schedule a new appointment using patient.appointments.build, do not want to create it via new action. In my browser the form is displayed via the app.dev/patients/3 (where 3 is the current patient view). This is where the appointments scheduling should take place. My appointments model, perhaps its controller does not know the current patient. This is what I believe to be the problem. Not sure how to write the appropriate code to define "current_patient in view". I suspect it is similar to console @current_patient = Patient.first
      singulars
    3. COokay, if you want to get current patient has ID is 3, like in your link above, try this: `@current_patient = Patient.where(id: params[:id])` in your create action, the `params[:id]` is id of patient in your current patient view. In console, you set `@current_patient` to **a Patient object**, otherwise in your create action, you set it to **a number**, so it just worked in console,
      singulars
 

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