ActiveRecord::AssociationTypeMismatch in ApplicationsController#create

(Apologies in advance...I can't seem to format this post
correctly...before answering, please let me know how to force
newlines/format code!!)

I've seen this error often and have tried to get to understand it based
on other posts, but to no avail.

I'm very new to Rails and so my lack of knowledge of the theoretical
framework here is likely the cause of this pitfall, but I would
absolutely love it if someone could please lead me up to what exactly
needs to change to avoid the error:

The exact error is:

Application(#70358560838160) expected, got String(#18246440)

Extracted source (around line #27):

# POST /applications.json
def create
   @application = Application.new(application_params)

   respond_to do |format|
     if @application.save

The parameters are as follows:

{"utf8"=>"✓",

"authenticity_token"=>"wqSTW1Blj7c7sdlx1VCuvxDxqaCeT3FnaHa3yk/98NF7tym4YOc+9EMxlagj5/cgwCa3ZyjLsIxKJG/jGVCV1w==",

"application"=>{"patients_ptID"=>"1",
"pharm_manufacturers_phID"=>"1",
"medications_rxnorm_ndc"=>"Tylenol",
"app_status"=>"Pending",
"date_init(1i)"=>"2015",
"date_init(2i)"=>"2",
"date_init(3i)"=>"8"},
"commit"=>"Create Application"}

Here is my ApplicationsController (applications_controller.rb):

class ApplicationsController < ApplicationController
  before_action :set_application, only: [:show, :edit, :update,
:destroy]

  # GET /applications
  # GET /applications.json
  def index
    @applications = Application.all
  end

  # GET /applications/1
  # GET /applications/1.json
  def show
  end

  # GET /applications/new
  def new
    @application = Application.new
  end

  # GET /applications/1/edit
  def edit
  end

  # POST /applications
  # POST /applications.json
  def create
    @application = Application.new(application_params)

    respond_to do |format|
      if @application.save
        format.html { redirect_to @application, notice: 'Application was
successfully created.' }
        format.json { render :show, status: :created, location:
@application }
      else
        format.html { render :new }
        format.json { render json: @application.errors, status:
:unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /applications/1
  # PATCH/PUT /applications/1.json
  def update
    respond_to do |format|
      if @application.update(application_params)
        format.html { redirect_to @application, notice: 'Application was
successfully updated.' }
        format.json { render :show, status: :ok, location: @application
}
      else
        format.html { render :edit }
        format.json { render json: @application.errors, status:
:unprocessable_entity }
      end
    end
  end

  # DELETE /applications/1
  # DELETE /applications/1.json
  def destroy
    @application.destroy
    respond_to do |format|
      format.html { redirect_to applications_url, notice: 'Application
was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between
actions.
    def set_application
      @application = Application.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the
white list through.
    def application_params
      params.require(:application).permit(:patients_ptID,
:pharm_manufacturers_phID, :medications_rxnorm_ndc, :app_status,
:date_init)
    end
end

And my applications form (applications/_form.html.erb):

<%= form_for(@application) do |f| %>
  <% if @application.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@application.errors.count, "error") %>
prohibited this application from being saved:</h2>

      <ul>
      <% @application.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :patients_ptID %><br>
    <%= f.number_field :patients_ptID %>
  </div>
  <div class="field">
    <%= f.label :pharm_manufacturers_phID %><br>
    <%= f.number_field :pharm_manufacturers_phID %>
  </div>
  <div class="field">
    <%= f.label :medications_rxnorm_ndc %><br>
    <%= f.text_field :medications_rxnorm_ndc %>
  </div>
  <div class="field">
    <%= f.label :app_status %><br>
    <%= f.text_field :app_status %>
  </div>
  <div class="field">
    <%= f.label :date_init %><br>
    <%= f.date_select :date_init %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

And patient.rb:

class Patient < ActiveRecord::Base
  belongs_to :application, foreign_key: "ptID", class_name:
"Application"
  belongs_to :dispensed_meds, foreign_key: "ptID", class_name:
"DispensedMed"
  belongs_to :prescriptions, foreign_key: "ptID", class_name:
"Prescription"
end

And application.rb:

class Application < ActiveRecord::Base
  has_one :medications_rx_norm
  has_one :medications_rxnorm_ndc, :through => :medications_rx_norm,
:source => :application, dependent: :nullify

  accepts_nested_attributes_for :medications_rx_norm, :allow_destroy =>
:true

  has_one :patient
  has_one :patients_ptID, :through => :patient, :source => :application,
dependent: :nullify

  accepts_nested_attributes_for :patient, :allow_destroy => :true

  has_one :pharm_manufacturer
  has_one :pharm_manufacturers_phID, :through => :pharm_manufacturer,
:source => :application, dependent: :nullify

  accepts_nested_attributes_for :pharm_manufacturer, :allow_destroy =>
:true

end

Please let me know ASAP if you need anything else!!

(Apologies in advance...I can't seem to format this post
correctly...before answering, please let me know how to force
newlines/format code!!)

That is more a matter of knowing how to control your email program is
it not? We can't really help with that here.

I've seen this error often and have tried to get to understand it based
on other posts, but to no avail.

I'm very new to Rails and so my lack of knowledge of the theoretical
framework here is likely the cause of this pitfall, but I would
absolutely love it if someone could please lead me up to what exactly
needs to change to avoid the error:

The exact error is:

Application(#70358560838160) expected, got String(#18246440)

Extracted source (around line #27):

# POST /applications.json
def create
   @application = Application.new(application_params)

I suspect, though not certain, that Application may be a reserved
word. Apart from anything else are you not going to get confused with
a file application_controller.rb and another
applications_controller.rb?

I suggest trying a different model name.

Colin

Thanks so much for the quick reply!

I figured out what was wrong with the formatting of the post (it was
just not showing up correctly in the preview).

That could be very likely! I will go back and change the model name and
get back to you as soon as I complete that!

Thanks again!

OK...so I renamed the table/model/controller and am getting a different
error based (likely) on the fact that I forgot to change the name
somewhere...

NameError in PatientApplicationsController#create

undefined local variable or method `patient_application_params' for
#<PatientApplicationsController:0x007f257dbdc9d8>

# POST /patient_applications.json
def create
   @patient_application = PatientApplication.new(patient_application_params)

   respond_to do |format|
     if @patient_application.save

The parameters are as follows:

{"utf8"=>"✓",

"authenticity_token"=>"Rf9oXjSWGjhsPlc9MHMxKEFWPjHSS5sAqDA0Pdsi9nVO+qrlMgDsBEtObej9anvfcI42nigMUHmZ3cWQHbjiRA==",

"patient_application"=>{"patients_ptID"=>"1",
"pharm_manufacturers_phID"=>"1",
"medications_rxnorm_ndc"=>"Tylenol",
"app_status"=>"Pending",
"date_init(1i)"=>"2015",
"date_init(2i)"=>"2",
"date_init(3i)"=>"8"},
"commit"=>"Create Patient application"}

The updated controller is as follows:

class PatientApplicationsController < ApplicationController
  before_action :set_patient_application, only: [:show, :edit, :update,
:destroy]

  # GET /patient_applications
  # GET /patient_applications.json
  def index
    @patient_applications = PatientApplication.all
  end

  # GET /patient_applications/1
  # GET /patient_applications/1.json
  def show
  end

  # GET /patient_applications/new
  def new
    @patient_application = PatientApplication.new
  end

  # GET /patient_applications/1/edit
  def edit
  end

  # POST /patient_applications
  # POST /patient_applications.json
  def create
    @patient_application =
PatientApplication.new(patient_application_params)

    respond_to do |format|
      if @patient_application.save
        format.html { redirect_to @patient_application, notice: 'Patient
application was successfully created.' }
        format.json { render :show, status: :created, location:
@patient_application }
      else
        format.html { render :new }
        format.json { render json: @patient_application.errors, status:
:unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /patient_applications/1
  # PATCH/PUT /patient_applications/1.json
  def update
    respond_to do |format|
      if @patient_application.update(application_params)
        format.html { redirect_to @patient_application, notice: 'Patient
application was successfully updated.' }
        format.json { render :show, status: :ok, location:
@patient_application }
      else
        format.html { render :edit }
        format.json { render json: @patient_application.errors, status:
:unprocessable_entity }
      end
    end
  end

  # DELETE /patient_applications/1
  # DELETE /patient_applications/1.json
  def destroy
    @patient_application.destroy
    respond_to do |format|
      format.html { redirect_to patient_applications_url, notice:
'Patient application was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between
actions.
    def set_patient_application
      @patient_application = PatientApplication.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the
white list through.
    def application_params
      params.require(:patient_application).permit(:patients_ptID,
:pharm_manufacturers_phID, :medications_rxnorm_ndc, :app_status,
:date_init)
    end
end

However, the original error persists in every connected table I
have...such as the following:

ActiveRecord::AssociationTypeMismatch in DispensedMedsController#create

DispensedMed(#69899493618640) expected, got String(#8997680)

# POST /dispensed_meds.json
def create
   @dispensed_med = DispensedMed.new(dispensed_med_params)

   respond_to do |format|
     if @dispensed_med.save

The controller as follows:

class DispensedMedsController < ApplicationController
  before_action :set_dispensed_med, only: [:show, :edit, :update,
:destroy]

  # GET /dispensed_meds
  # GET /dispensed_meds.json
  def index
    @dispensed_meds = DispensedMed.all
  end

  # GET /dispensed_meds/1
  # GET /dispensed_meds/1.json
  def show
  end

  # GET /dispensed_meds/new
  def new
    @dispensed_med = DispensedMed.new
  end

  # GET /dispensed_meds/1/edit
  def edit
  end

  # POST /dispensed_meds
  # POST /dispensed_meds.json
  def create
    @dispensed_med = DispensedMed.new(dispensed_med_params)

    respond_to do |format|
      if @dispensed_med.save
        format.html { redirect_to @dispensed_med, notice: 'Dispensed med
was successfully created.' }
        format.json { render :show, status: :created, location:
@dispensed_med }
      else
        format.html { render :new }
        format.json { render json: @dispensed_med.errors, status:
:unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /dispensed_meds/1
  # PATCH/PUT /dispensed_meds/1.json
  def update
    respond_to do |format|
      if @dispensed_med.update(dispensed_med_params)
        format.html { redirect_to @dispensed_med, notice: 'Dispensed med
was successfully updated.' }
        format.json { render :show, status: :ok, location:
@dispensed_med }
      else
        format.html { render :edit }
        format.json { render json: @dispensed_med.errors, status:
:unprocessable_entity }
      end
    end
  end

  # DELETE /dispensed_meds/1
  # DELETE /dispensed_meds/1.json
  def destroy
    @dispensed_med.destroy
    respond_to do |format|
      format.html { redirect_to dispensed_meds_url, notice: 'Dispensed
med was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between
actions.
    def set_dispensed_med
      @dispensed_med = DispensedMed.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the
white list through.
    def dispensed_med_params
      params.require(:dispensed_med).permit(:dis_date, :patients_ptID,
:inventory_invID)
    end
end

And the respective form:

<%= form_for(@dispensed_med) do |f| %>
  <% if @dispensed_med.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@dispensed_med.errors.count, "error") %>
prohibited this dispensed_med from being saved:</h2>

      <ul>
      <% @dispensed_med.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :dis_date %><br>
    <%= f.date_select :dis_date %>
  </div>
  <div class="field">
    <%= f.label :patients_ptID %><br>
    <%= f.number_field :patients_ptID %>
  </div>
  <div class="field">
    <%= f.label :inventory_invID %><br>
    <%= f.number_field :inventory_invID %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

As well as the model:

class DispensedMed < ActiveRecord::Base
  has_one :patient
  has_one :patients_ptID, :through => :patient, :source =>
:dispensed_meds, dependent: :nullify
  has_one :inventory
  has_one :inventory_invID, :through => :inventory, :source =>
:dispensed_meds, dependent: :nullify
end

Once again, thanks so so so much. It means a ton.