Rails 5:弃用警告:您正在将 ActiveRecord::Base 的实例传递给“存在?”。 ..我不知道
Rails 5: DEPRECATION WARNING: You are passing an instance of ActiveRecord::Base to `exists?`. ..I have no idea
好的....我有一个模式中的表单要提交。我不明白我收到的警告/错误阻止 POST 发生。我错过了什么?
这是让我抓狂的剥夺警告
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)
请注意没有“存在”?在第 35 行(见下文)。
这是第 35 行标记为
的控制器操作
# 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
如果我用 Pry 停止它以查看对象通过我看到...
撬控制台输出
* 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" = LIMIT [["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>)>
这东西要我干什么?我错过了什么?
如果结果是没有数据被保存到数据库中,而是有一个ROLLBACK。然后从我的角度来看,有些东西坏了,这是一个“错误”。或者更确切地说,“错误”已经发生,因为结果不是所期望的。
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" = LIMIT [["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" = LIMIT [["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" = 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"
执行停止,没有任何内容发布到我的数据库。
Contact Exists (0.4ms) SELECT 1 AS one FROM "contacts" WHERE "contacts"."id" = LIMIT [["id", 166574], ["LIMIT", 1]]
这告诉您您的联系人已经存在,因此如果您希望该代码起作用,请创建一个尚不存在的联系人,或者更改执行此检查的模型代码或数据库模式(如果您有)一个独特的 属性 集,您的代码中没有错误,警告只是弃用警告而不是错误。
您似乎正在访问一条已经存在的记录,所以您可能想要更新而不是创建?这可能是逻辑错误而不是编码错误,但只有您知道您要做什么
好的....我有一个模式中的表单要提交。我不明白我收到的警告/错误阻止 POST 发生。我错过了什么?
这是让我抓狂的剥夺警告
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)
请注意没有“存在”?在第 35 行(见下文)。 这是第 35 行标记为
的控制器操作 # 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
如果我用 Pry 停止它以查看对象通过我看到... 撬控制台输出
* 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" = LIMIT [["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>)>
这东西要我干什么?我错过了什么?
如果结果是没有数据被保存到数据库中,而是有一个ROLLBACK。然后从我的角度来看,有些东西坏了,这是一个“错误”。或者更确切地说,“错误”已经发生,因为结果不是所期望的。
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" = LIMIT [["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" = LIMIT [["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" = 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"
执行停止,没有任何内容发布到我的数据库。
Contact Exists (0.4ms) SELECT 1 AS one FROM "contacts" WHERE "contacts"."id" = LIMIT [["id", 166574], ["LIMIT", 1]]
这告诉您您的联系人已经存在,因此如果您希望该代码起作用,请创建一个尚不存在的联系人,或者更改执行此检查的模型代码或数据库模式(如果您有)一个独特的 属性 集,您的代码中没有错误,警告只是弃用警告而不是错误。 您似乎正在访问一条已经存在的记录,所以您可能想要更新而不是创建?这可能是逻辑错误而不是编码错误,但只有您知道您要做什么