Rails 5: DEPRECATION WARNING: You are passing an instance of ActiveRecord::Base to `exists?`. ..I have no idea

Ok….I have a form in a modal that I am trying to submit. I do not understand the Warning / error I am getting that is keeping the POST from taking place. What am I missing?

Here is the Deprication Warning that is drivin me nuts

DEPRECATION WARNING: You are passing an instance of ActiveRecord::Base to `exists?`. Please pass the id of the object by calling `.id`. (called from block in create at /home/fonso/back-experiment/app/controllers/assignments_controller.rb:35)

Note that there is no "exists?" on line 35 (see below).
This is the controller action with line 35 marked

  # POST /assignments or /assignments.json
  def create
    @assignment = Assignment.new(assignment_params)

    # error wants contact.id not contact_id ???

    respond_to do |format|
      if @assignment.save  #<-------------------LINE 35
        format.html { redirect_to @assignment, notice: "Assignment was successfully created." }
        format.json { render :show, status: :created, location: @assignment }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @assignment.errors, status: :unprocessable_entity }
      end
    end
  end

If I stop it with Pry to see the object getting passed I see…
Pry console output

* Environment: development
* Listening on tcp://0.0.0.0:4000
Use Ctrl-C to stop
Started POST "/assignments" for 22.222.22.222 at 2021-11-05 20:34:11 -0700
Cannot render console from 22.222.22.222! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
  ActiveRecord::SchemaMigration Load (1.3ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by AssignmentsController#create as JS
  Parameters: {"utf8"=>"✓", "volunteer_task_type_id"=>"41", "roster_id"=>"7", "program_id"=>"9", "set_description"=>["fonso nov5 test"], "set_date"=>["2021-01-05"], "assignment"=>{"start_time(1i)"=>"2021", "start_time(2i)"=>"11", "start_time(3i)"=>"6", "start_time(4i)"=>"08", "start_time(5i)"=>"00", "end_time(1i)"=>"2021", "end_time(2i)"=>"11", "end_time(3i)"=>"6", "end_time(4i)"=>"09", "end_time(5i)"=>"00", "notes"=>"nov5", "contact_id"=>"166574", "closed"=>"1", "lock_version"=>"0"}, "contact_element_prefix"=>"contact", "commit"=>"Submit"}
  Contact Load (0.2ms)  SELECT  "contacts".* FROM "contacts" WHERE "contacts"."id" = $1 LIMIT $2  [["id", 166574], ["LIMIT", 1]]

From: /home/fonso/back-experiment/app/controllers/assignments_controller.rb @ line 31 in AssignmentsController#create:

    26:   end
    27: 
    28:   # POST /assignments or /assignments.json
    29:   def create
    30:     @assignment = Assignment.new(assignment_params)
 => 31:     binding.pry
    32:     # error wants contact.id not contact_id ???
    33:     # try the create_shift code here??
    34:     # NOTE: comment original out 4 now <--- WHY? this saves add_shift now
    35: 
    36:     respond_to do |format|
pry(#<AssignmentsController>)> @assignment
=> #<Assignment:0x00007f641862e610
 id: nil,
 volunteer_shift_id: nil,
 contact_id: 166574,
 created_at: nil,
 updated_at: nil,
 attendance_type_id: nil,
 notes: "nov5",
 call_status_type_id: nil,
 closed: true,
 lock_version: 0,
 color: nil,
 start_time: Sat, 06 Nov 2021 08:00:00 UTC +00:00,
 end_time: Sat, 06 Nov 2021 09:00:00 UTC +00:00>
pry(#<AssignmentsController>)> 

What is this thing asking me for? What am I missing?

If the outcome is that no data is saved to the database and instead there is a ROLLBACK. Then from my perspective something is broken and this is an "error". Or rather and "error" has occurred in that the outcome is not what is desired.

Processing by AssignmentsController#create as JS
  Parameters: {"utf8"=>"✓", "volunteer_task_type_id"=>"41", "roster_id"=>"7", "program_id"=>"9", "set_description"=>["fonso nov5 test"], "set_date"=>["2021-01-05"], "assignment"=>{"start_time(1i)"=>"2021", "start_time(2i)"=>"11", "start_time(3i)"=>"6", "start_time(4i)"=>"08", "start_time(5i)"=>"00", "end_time(1i)"=>"2021", "end_time(2i)"=>"11", "end_time(3i)"=>"6", "end_time(4i)"=>"09", "end_time(5i)"=>"00", "notes"=>"nov5", "contact_id"=>"166574", "closed"=>"1", "lock_version"=>"0"}, "contact_element_prefix"=>"contact", "commit"=>"Submit"}
  Contact Load (0.2ms)  SELECT  "contacts".* FROM "contacts" WHERE "contacts"."id" = $1 LIMIT $2  [["id", 166574], ["LIMIT", 1]]
   (0.2ms)  BEGIN
DEPRECATION WARNING: You are passing an instance of ActiveRecord::Base to `exists?`. Please pass the id of the object by calling `.id`. (called from block in create at /home/fonso/back-experiment/app/controllers/assignments_controller.rb:35)
  Contact Exists (0.4ms)  SELECT  1 AS one FROM "contacts" WHERE "contacts"."id" = $1 LIMIT $2  [["id", 166574], ["LIMIT", 1]]
   (0.2ms)  ROLLBACK
  Rendering assignments/new.html.erb within layouts/application
  VolunteerTaskType Load (0.6ms)  SELECT "volunteer_task_types".* FROM "volunteer_task_types" WHERE "volunteer_task_types"."instantiable" = $1 AND ((effective_on IS NULL OR effective_on <= '2021-11-05') AND (ineffective_on IS NULL OR ineffective_on > '2021-11-05'))  [["instantiable", "t"]]
  Roster Load (0.4ms)  SELECT "rosters".* FROM "rosters"


Execution STOPS and nothing is posted to my database.

>Solution :

Contact Exists (0.4ms) SELECT 1 AS one FROM "contacts" WHERE "contacts"."id" = $1 LIMIT $2 [["id", 166574], ["LIMIT", 1]]

This is telling you that your contact already exists so if you want that code to work then create a contact that does not already exist or change your models code that does this check or the database schema if that is where you have a unique property set, you have NO error in your code, the warning is just a deprecation warning not an error.
You seem to be accessing a record that already exists so maybe you are wanting to update rather than create? this may be a logic error rather than a coding error but only you know what it is you are trying to do

Leave a Reply