From 22a169740ef0acc985420db407605718a038a489 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Mon, 9 Dec 2019 19:08:07 +0800 Subject: [PATCH 1/4] 1 --- app/controllers/hacks_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/hacks_controller.rb b/app/controllers/hacks_controller.rb index 4465d515d..a71f9942d 100644 --- a/app/controllers/hacks_controller.rb +++ b/app/controllers/hacks_controller.rb @@ -68,7 +68,7 @@ class HacksController < ApplicationController @hack.hack_sets.create!(hack_sets_params) # 更新代码 if params[:hack_codes][:code] != @hack.code - hack_code_params = hack_code_params.merge(modify_time: Time.now) + hack_code_params.merge!(modify_time: Time.now) end @hack.hack_codes.create!(hack_code_params) end From 47722dd8864b622e1240f9ebd5203c5afe61a4fb Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Mon, 9 Dec 2019 19:21:13 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E7=BC=96=E8=BE=91=E5=92=8C=E6=96=B0?= =?UTF-8?q?=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/hacks_controller.rb | 9 ++++++--- db/migrate/20191209111347_add_index_for_hack_codes.rb | 6 ++++++ 2 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20191209111347_add_index_for_hack_codes.rb diff --git a/app/controllers/hacks_controller.rb b/app/controllers/hacks_controller.rb index a71f9942d..e2023d78d 100644 --- a/app/controllers/hacks_controller.rb +++ b/app/controllers/hacks_controller.rb @@ -48,7 +48,9 @@ class HacksController < ApplicationController hack.save! # 创建测试集与代码 hack.hack_sets.create!(hack_sets_params) - hack.hack_codes.create!(hack_code_params.merge(modify_time: Time.now)) + hack_codes = hack.hack_codes.new(hack_code_params) + hack_codes.modify_time = Time.now + hack_codes.save! end render_ok({identifier: hack.identifier}) rescue Exception => e @@ -67,10 +69,11 @@ class HacksController < ApplicationController # 新建 @hack.hack_sets.create!(hack_sets_params) # 更新代码 + hack_codes = @hack.hack_codes.new(hack_code_params) if params[:hack_codes][:code] != @hack.code - hack_code_params.merge!(modify_time: Time.now) + hack_codes.modify_time = Time.now end - @hack.hack_codes.create!(hack_code_params) + hack_codes.save! end render_ok rescue Exception => e diff --git a/db/migrate/20191209111347_add_index_for_hack_codes.rb b/db/migrate/20191209111347_add_index_for_hack_codes.rb new file mode 100644 index 000000000..f1e655e21 --- /dev/null +++ b/db/migrate/20191209111347_add_index_for_hack_codes.rb @@ -0,0 +1,6 @@ +class AddIndexForHackCodes < ActiveRecord::Migration[5.2] + def change + HackCode.destroy_all + add_index :hack_codes, [:hack_id, :language], unique: true + end +end From d97f76952ce03476a0694a06dce2101858983533 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Mon, 9 Dec 2019 20:41:26 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=87=BA=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/hacks_controller.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/controllers/hacks_controller.rb b/app/controllers/hacks_controller.rb index e2023d78d..2cacc362e 100644 --- a/app/controllers/hacks_controller.rb +++ b/app/controllers/hacks_controller.rb @@ -69,11 +69,10 @@ class HacksController < ApplicationController # 新建 @hack.hack_sets.create!(hack_sets_params) # 更新代码 - hack_codes = @hack.hack_codes.new(hack_code_params) if params[:hack_codes][:code] != @hack.code - hack_codes.modify_time = Time.now + hack_code_params = hack_code_params.merge(modify_time: Time.now) end - hack_codes.save! + @hack.hack_codes.first.update_attributes!(hack_code_params) end render_ok rescue Exception => e From 1744102a5bfc7303c4b8f1a249f620a770c26070 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Mon, 9 Dec 2019 20:43:56 +0800 Subject: [PATCH 4/4] 1 --- app/controllers/hacks_controller.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/controllers/hacks_controller.rb b/app/controllers/hacks_controller.rb index 2cacc362e..c9e9097f8 100644 --- a/app/controllers/hacks_controller.rb +++ b/app/controllers/hacks_controller.rb @@ -69,10 +69,8 @@ class HacksController < ApplicationController # 新建 @hack.hack_sets.create!(hack_sets_params) # 更新代码 - if params[:hack_codes][:code] != @hack.code - hack_code_params = hack_code_params.merge(modify_time: Time.now) - end - @hack.hack_codes.first.update_attributes!(hack_code_params) + code_params = params[:hack_codes][:code] != @hack.code ? hack_code_params.merge(modify_time: Time.now) : hack_code_params + @hack.hack_codes.first.update_attributes!(code_params) end render_ok rescue Exception => e