记录仅节省20%的时间

时间:2019-01-30 16:15:51

标签: ruby-on-rails postgresql

我有一个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

1 个答案:

答案 0 :(得分:1)

您的Project模型和Subproject模型都要求名称唯一。这意味着,如果有人添加具有相同名称的项目,它将无法保存。对于Subproject来说,这也是一个问题。

因此,如果我尝试创建一个名为“ Branding Update”的项目,并且该项目的子项目为“ 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