如何在控制台中创建记录 - ROLLBACK TO SAVEPOINT active_record_1?

时间:2017-08-18 00:29:00

标签: ruby-on-rails activerecord

我是rails的新手,在尝试创建成本代码时不确定错误是什么:

[21] pry(main)> CostCode
=> CostCode(id: integer, biller_type: text, biller_id: integer, position: integer, parent_id: integer, code: text, name: text, updated_at: datetime, long_name_helper: text, deleted_at: datetime, sortable_code: text, created_at: datetime, standard_cost_code_id: integer)

[22] pry(main)> CostCode.create(code: '87678', name: "hex")
   (0.3ms)  SAVEPOINT active_record_1
   (0.2ms)            SET LOCAL procore.user_id='';
          SET LOCAL procore.company_id='';
          SET LOCAL procore.project_id='';

  CostCode Exists (0.4ms)  SELECT  1 AS one FROM "cost_codes" WHERE ("cost_codes"."code" = '87678' AND "cost_codes"."biller_type" IS NULL AND "cost_codes"."biller_id" IS NULL AND "cost_codes"."parent_id" IS NULL AND "cost_codes"."deleted_at" IS NULL) LIMIT 1
   (0.2ms)  ROLLBACK TO SAVEPOINT active_record_1
=> #<CostCode:0x007ff4cd938ec8
 id: nil,
 biller_type: nil,
 biller_id: nil,
 position: nil,
 parent_id: nil,
 code: "87678",
 name: "hex",
 updated_at: nil,
 long_name_helper: nil,
 deleted_at: nil,
 sortable_code: nil,
 created_at: nil,
 standard_cost_code_id: nil>
[23] pry(main)>

为什么这条记录不能保存,并且仅此错误消息提供了足够的详细信息以了解我缺少的内容?或者我还应该从模型中寻找其他东西吗?

以下是CostCode模型的代码:

    # Table name: cost_codes
    #
    #  id                    :integer          not null, primary key
    #  biller_type           :text
    #  biller_id             :integer
    #  position              :integer
    #  parent_id             :integer
    #  code                  :text
    #  name                  :text
    #  updated_at            :datetime
    #  long_name_helper      :text
    #  deleted_at            :datetime
    #  sortable_code         :text
    #  created_at            :datetime         not null
    #  standard_cost_code_id :integer
    #
    # Indexes
    #
    #  cost_codes_parent_id_index                     (parent_id)
    #  idx_cost_codes_on_code_biller_null_parent      (code,biller_type,biller_id) UNIQUE
    #  idx_cost_codes_on_code_biller_parent           (code,biller_type,biller_id,parent_id) UNIQUE
    #  idxcost_codes_biller_id                        (biller_id)
    #  index_cost_codes_on_biller_id_and_biller_type  (biller_id,biller_type)
    #  index_cost_codes_on_standard_cost_code_id      (standard_cost_code_id)
    #

    class CostCode < ActiveRecord::Base
      class ExistingCostCodesForHolder < StandardError
      end

      DEFAULT_COST_CODES = YAML.load_file(Rails.root.join('config', 'cost_codes_17_division.yml'))

      acts_as_procore_relatable
      acts_as_tree # but really, it's pro_tree
      acts_as_paranoid

      include ChangeEventCostCodesCaching
      include ExternalDataSupport
      include ReplicaSupport

      origin_id_unique_within { |cost_code| ['company_id', cost_code.company.id] }

      belongs_to :biller, :polymorphic => true
      belongs_to :standard_cost_code
      has_one    :erp_cost_code, class_name: "Erp::CostCode", foreign_key: :procore_cost_code_id, dependent: :destroy
      has_many   :erp_sync_errors, class_name: 'Erp::SyncError', as: :sync_item, dependent: :destroy
      has_one    :erp_standard_cost_code, through: :standard_cost_code
      has_many   :potential_change_orders
      has_many   :quantity_logs
      has_many   :generic_tool_items
      has_many   :meeting_topics
      has_many   :timecard_entries
      has_many   :budget_forecast_modifications
      has_many   :budget_line_items, :dependent => :restrict_with_error
      has_many   :line_items, dependent: :restrict_with_error
      has_one    :bid_item

1 个答案:

答案 0 :(得分:0)

创建记录时,您必须确保提供所有外键以及通过所有验证。

在你的情况下:

你有2个外键约束:

belongs_to :biller, :polymorphic => true
belongs_to :standard_cost_code

以及一些数据库级验证:

idx_cost_codes_on_code_biller_null_parent      (code,biller_type,biller_id) UNIQUE
idx_cost_codes_on_code_biller_parent           (code,biller_type,biller_id,parent_id) UNIQUE

所以你必须写这样的东西来创建一个记录:

CostCode.create(biller: <Biller instance>, standard_cost_code: <StandardCostCode instance>, code: '87678', name: "hex")

确保BillerStandardCostCode已经存在。