记录仅节省20%的时间
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了记录仅节省20%的时间相关的知识,希望对你有一定的参考价值。
我有一个模型“项目”,has_many: Subprojects, through: Project_Associations
。
基本上,当用户创建项目时,他们可以选择子项目或适用于它的项目的较小组件。这在我的开发环境中工作正常,项目可以正常保存,但在生产中的Heroku用户经常报告他们无法保存项目,除非他们不检查任何子项目。我遇到了同样的问题,但我也曾经有过很好的工作时间。
这是日志中的转储,当它不起作用时:
Started POST "/projects" for 205.167.40.131 at 2019-01-29 13:59:57 +0000
2019-01-29T13:59:57.133251+00:00 app[web.1]: I, [2019-01-29T13:59:57.133172 #16] INFO -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Processing by ProjectsController#create as html
2019-01-29T13:59:57.137677+00:00 app[web.1]: I, [2019-01-29T13:59:57.137577 #16] INFO -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Parameters: {"utf8"=>"✓", "authenticity_token"=>"QJhasohAhfaoih919BoPIIGPdqhxinginSHian5d+Gg==+", "project"=>{"name"=>"New Project Name", "summary"=>"Project Summary ", "go_live"=>"2019-06-15", "precentage_complete"=>"0", "start"=>"2019-01-17", "subproject_ids"=>["", "4", "1", "8", "10", "9", "13", "11"]}, "Start Project"=>"Create Project"}
2019-01-29T13:59:57.147658+00:00 app[web.1]: D, [2019-01-29T13:59:57.147534 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] User Load (3.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 51], ["LIMIT", 1]]
2019-01-29T13:59:57.152028+00:00 app[web.1]: D, [2019-01-29T13:59:57.151925 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Subproject Load (1.4ms) SELECT "subprojects".* FROM "subprojects" WHERE "subprojects"."id" IN (4, 1, 8, 10, 9, 13, 11)
2019-01-29T13:59:57.176569+00:00 app[web.1]: D, [2019-01-29T13:59:57.176430 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] (10.6ms) BEGIN
2019-01-29T13:59:57.181619+00:00 app[web.1]: D, [2019-01-29T13:59:57.181485 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Subproject Exists (1.0ms) SELECT 1 AS one FROM "subprojects" WHERE "subprojects"."name" = $1 AND ("subprojects"."id" != $2) LIMIT $3 [["name", "Updates"], ["id", 4], ["LIMIT", 1]]
2019-01-29T13:59:57.185301+00:00 app[web.1]: D, [2019-01-29T13:59:57.185215 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Subproject Exists (1.1ms) SELECT 1 AS one FROM "subprojects" WHERE "subprojects"."name" = $1 AND ("subprojects"."id" != $2) LIMIT $3 [["name", "Communication"], ["id", 1], ["LIMIT", 1]]
2019-01-29T13:59:57.188964+00:00 app[web.1]: D, [2019-01-29T13:59:57.188877 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Subproject Exists (1.1ms) SELECT 1 AS one FROM "subprojects" WHERE "subprojects"."name" = $1 AND ("subprojects"."id" != $2) LIMIT $3 [["name", "Project Meeting"], ["id", 8], ["LIMIT", 1]]
2019-01-29T13:59:57.197124+00:00 app[web.1]: D, [2019-01-29T13:59:57.197011 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Subproject Exists (4.6ms) SELECT 1 AS one FROM "subprojects" WHERE "subprojects"."name" = $1 AND ("subprojects"."id" != $2) LIMIT $3 [["name", "Training Delivery"], ["id", 10], ["LIMIT", 1]]
2019-01-29T13:59:57.204607+00:00 app[web.1]: D, [2019-01-29T13:59:57.204479 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Subproject Exists (6.0ms) SELECT 1 AS one FROM "subprojects" WHERE "subprojects"."name" = $1 AND ("subprojects"."id" != $2) LIMIT $3 [["name", "Training Design"], ["id", 9], ["LIMIT", 1]]
2019-01-29T13:59:57.208083+00:00 app[web.1]: D, [2019-01-29T13:59:57.207989 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Subproject Exists (2.0ms) SELECT 1 AS one FROM "subprojects" WHERE "subprojects"."name" = $1 AND ("subprojects"."id" != $2) LIMIT $3 [["name", "Training Development"], ["id", 13], ["LIMIT", 1]]
2019-01-29T13:59:57.233123+00:00 app[web.1]: D, [2019-01-29T13:59:57.232977 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Subproject Exists (14.9ms) SELECT 1 AS one FROM "subprojects" WHERE "subprojects"."name" = $1 AND ("subprojects"."id" != $2) LIMIT $3 [["name", "Train the Trainer"], ["id", 11], ["LIMIT", 1]]
2019-01-29T13:59:57.247200+00:00 app[web.1]: D, [2019-01-29T13:59:57.247058 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Project Exists (4.3ms) SELECT 1 AS one FROM "projects" WHERE "projects"."name" = $1 LIMIT $2 [["name", "Upfront Examiner Adjudication"], ["LIMIT", 1]]
2019-01-29T13:59:57.251205+00:00 app[web.1]: D, [2019-01-29T13:59:57.251106 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] (2.8ms) ROLLBACK
2019-01-29T13:59:57.252262+00:00 app[web.1]: D, [2019-01-29T13:59:57.252173 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] (0.5ms) BEGIN
2019-01-29T13:59:57.257433+00:00 app[web.1]: D, [2019-01-29T13:59:57.257342 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Subproject Exists (0.7ms) SELECT 1 AS one FROM "subprojects" WHERE "subprojects"."name" = $1 AND ("subprojects"."id" != $2) LIMIT $3 [["name", "Updates"], ["id", 4], ["LIMIT", 1]]
2019-01-29T13:59:57.259329+00:00 app[web.1]: D, [2019-01-29T13:59:57.259246 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Subproject Exists (0.6ms) SELECT 1 AS one FROM "subprojects" WHERE "subprojects"."name" = $1 AND ("subprojects"."id" != $2) LIMIT $3 [["name", "Communication"], ["id", 1], ["LIMIT", 1]]
2019-01-29T13:59:57.263853+00:00 app[web.1]: D, [2019-01-29T13:59:57.263763 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Subproject Exists (1.9ms) SELECT 1 AS one FROM "subprojects" WHERE "subprojects"."name" = $1 AND ("subprojects"."id" != $2) LIMIT $3 [["name", "Project Meeting"], ["id", 8], ["LIMIT", 1]]
2019-01-29T13:59:57.267172+00:00 app[web.1]: D, [2019-01-29T13:59:57.267079 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Subproject Exists (2.1ms) SELECT 1 AS one FROM "subprojects" WHERE "subprojects"."name" = $1 AND ("subprojects"."id" != $2) LIMIT $3 [["name", "Training Delivery"], ["id", 10], ["LIMIT", 1]]
2019-01-29T13:59:57.272418+00:00 app[web.1]: D, [2019-01-29T13:59:57.272320 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Subproject Exists (4.0ms) SELECT 1 AS one FROM "subprojects" WHERE "subprojects"."name" = $1 AND ("subprojects"."id" != $2) LIMIT $3 [["name", "Training Design"], ["id", 9], ["LIMIT", 1]]
2019-01-29T13:59:57.274358+00:00 app[web.1]: D, [2019-01-29T13:59:57.274276 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Subproject Exists (0.7ms) SELECT 1 AS one FROM "subprojects" WHERE "subprojects"."name" = $1 AND ("subprojects"."id" != $2) LIMIT $3 [["name", "Training Development"], ["id", 13], ["LIMIT", 1]]
2019-01-29T13:59:57.280155+00:00 app[web.1]: D, [2019-01-29T13:59:57.280028 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Subproject Exists (0.7ms) SELECT 1 AS one FROM "subprojects" WHERE "subprojects"."name" = $1 AND ("subprojects"."id" != $2) LIMIT $3 [["name", "Train the Trainer"], ["id", 11], ["LIMIT", 1]]
2019-01-29T13:59:57.282022+00:00 app[web.1]: D, [2019-01-29T13:59:57.281944 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Project Exists (0.9ms) SELECT 1 AS one FROM "projects" WHERE "projects"."name" = $1 LIMIT $2 [["name", "Upfront Examiner Adjudication"], ["LIMIT", 1]]
2019-01-29T13:59:57.289942+00:00 app[web.1]: D, [2019-01-29T13:59:57.289832 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] (7.5ms) ROLLBACK
2019-01-29T13:59:57.291030+00:00 app[web.1]: I, [2019-01-29T13:59:57.290946 #16] INFO -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Rendering projects/new.html.erb within layouts/application
2019-01-29T13:59:57.299919+00:00 app[web.1]: D, [2019-01-29T13:59:57.299823 #16] DEBUG -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Subproject Load (2.6ms) SELECT "subprojects".* FROM "subprojects" ORDER BY "subprojects"."name" ASC
2019-01-29T13:59:57.309977+00:00 app[web.1]: I, [2019-01-29T13:59:57.309816 #16] INFO -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Rendered projects/_form.html.erb (18.5ms)
2019-01-29T13:59:57.310122+00:00 app[web.1]: I, [2019-01-29T13:59:57.310033 #16] INFO -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Rendered projects/new.html.erb within layouts/application (18.9ms)
2019-01-29T13:59:57.311921+00:00 app[web.1]: I, [2019-01-29T13:59:57.311825 #16] INFO -- : [0606ad52-0c91-4cb0-b60a-71048099e7a5] Completed 200 OK in 174ms (Views: 18.7ms | ActiveRecord: 75.8ms)
2019-01-29T13:59:57.318562+00:00 heroku[router]: at=info method=POST path="/projects" host=magic-trackr.herokuapp.com request_id=0606ad52-0c91-4cb0-b60a-71048099e7a5 fwd="205.167.40.131" dyno=web.1 connect=0ms service=189ms status=200 bytes=8018 protocol=https
来自projects_controller:
def new
@project = Project.new
end
def create
@project = Project.create(project_params)
if @project.save
flash[:success] = "Project started"
redirect_to tasks_path
else
flash[:error] = "project not added"
render 'new'
end
end
_Form.html.erb
<%= form_for(@project, html: {class: "form-group"}) do |f| %>
<%= render 'errors' %>
<%= f.label :name %>
<%= f.text_field :name, id: "project-name", class: "form-control" %>
<%= f.label :summary%>
<%= f.text_field :summary, id: "go-live", class: "form-control" %>
<%= f.label :go_live %>
<%= f.date_field :go_live, id: "go-live", class: "form-control" %>
<%= f.label :precentage_complete %>
<%= f.number_field :precentage_complete, id: "go-live", class: "form-control" %>
<%= f.label "Start Date" %>
<%= f.date_field :start, id: "date-field", class: "form-control" %>
<br />
<%= f.label "Included subprojects" %>
<br />
<ul class="list-group sub-task">
<%= f.collection_check_boxes(:subproject_ids, Subproject.order(:name), :id,
:name, {class: "form-control"}) do |box| %>
<li class="list-group-item">
<%= box.check_box + box.text %>
</li>
<% end %>
</ul>
<%= f.submit name: btn_name, class: 'btn btn-primary' %>
<% end %>
从查看成功保存的日志和不成功的日志,我无法真正看到记录没有保存的任何原因。但是,当用户将子项目添加到他们的项目时,总会遇到麻烦。
Project.rb
class Project < ApplicationRecord
has_many :tasks, dependent: :destroy
has_many :project_assocaitions
has_many :subprojects, through: :project_assocaitions
validates :name, uniqueness: true, presence: true, length: {minimum: 2}
Project_Assocaition.rb(我发现关联拼写错误:
class ProjectAssocaition < ApplicationRecord
belongs_to :project
belongs_to :subproject
end
SubProject.rb
class Subproject < ApplicationRecord
has_many :project_assocaitions
has_many :projects, through: :project_assocaitions
validates :name, uniqueness: true, presence: true, length: {minimum: 2}
end
你的Project
模型和Subproject
模型都要求名称是唯一的。这意味着如果有人添加了具有相同名称的项目,则无法保存。这也是Subproject
的一个问题。
因此,如果我尝试使用“Kickoff”和“Demo”子项目创建名为“Branding Update”的项目 - 如果数据库为空,这将成功。然后我创建另一个名为“新产品”的项目,该项目具有相同的命名子项目“Kickoff”和“Demo” - 这将无法成功保存,因为“Kickoff”和“Demo”需要是全球唯一的并且它们已经是用于“品牌更新”项目。
也许您想要在子项目中确定唯一性,如下所示:
class Subproject < ApplicationRecord
has_many :project_assocaitions
has_many :projects, through: :project_assocaitions
validates :name, uniqueness: {scope: :project_id, message: "should only be one per project"}, presence: true, length: {minimum: 2}
end
以上是关于记录仅节省20%的时间的主要内容,如果未能解决你的问题,请参考以下文章
译文:18个实用的JavaScript代码片段,助你快速处理日常编程任务