首页 > 解决方案 > 在控制器中创建操作省略了参考字段

问题描述

当我为环境创建客户时,我的 Rails 应用程序出现问题,如果省略environment_id插入语句中的参数,则操作本身存在问题。

这是我的customer.rb模型:

class Customer < ActiveRecord::Base
  belongs_to :environment, inverse_of: :customers

  has_many :all_services, class_name: 'Service'
  has_many :services, inverse_of: :customer

  has_paper_trail ignore: %i[created_at updated_at]
end

这是我的environment.rb模型:

class Environment < ActiveRecord::Base
  has_many :all_customers, class_name: 'Customer', dependent: :destroy
  has_many :customers, inverse_of: :environment

  has_many :all_services, class_name: 'Service', dependent: :destroy
  has_many :services, inverse_of: :environment

  has_many :all_versions, class_name: 'Version', dependent: :destroy
  has_many :versions, inverse_of: :environment

  has_many :all_role_permissions, class_name: 'RolePermission', dependent: :destroy
  has_many :role_permissions, inverse_of: :environment

  has_paper_trail ignore: %i[created_at updated_at]
end

这是customer_controller.rb创建操作:

def create
 if customer_params.permitted?
  render json: Customer.create!(customer_params), status: :ok
 else
  render json: { message: Api::V1::INVALID_PARAMETERS }, status: :bad_request
 end
end

def customer_params
 params.require(:customer).permit(:environment_id, :full_name, :document_type, :document_value, :customer_type)
end

这是服务器日志:

Started POST "/api/v1/customers" for 127.0.0.1 at 2021-06-02 18:18:44 +0100
Processing by Api::V1::CustomersController#create as HTML
  Parameters: {"full_name"=>"cvbcv", "document_type"=>"bcvbc", "document_value"=>"vbcvbcv", "customer_type"=>"bcvbcvb", "environment_id"=>1, "customer"=>{"full_name"=>"cvbcv", "document_type"=>"bcvbc", "document_value"=>"vbcvbcv", "customer_type"=>"bcvbcvb", "environment_id"=>1}}
  User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."uid" = $1 LIMIT $2  [["uid", "test1@test.cl"], ["LIMIT", 1]]
   (0.5ms)  BEGIN
  ↳ app/services/api/customers.rb:20:in `create_customer'
  Environment Load (0.5ms)  SELECT "environments".* FROM "environments" WHERE "environments"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ app/services/api/customers.rb:20:in `create_customer'
  Customer Create (1.4ms)  INSERT INTO "customers" ("full_name", "document_type", "document_value", "customer_type", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["full_name", "cvbcv"], ["document_type", "bcvbc"], ["document_value", "vbcvbcv"], ["customer_type", "bcvbcvb"], ["created_at", "2021-06-02 17:18:44.378509"], ["updated_at", "2021-06-02 17:18:44.378509"]]
  ↳ app/services/api/customers.rb:20:in `create_customer'
   (1.2ms)  ROLLBACK
  ↳ app/services/api/customers.rb:20:in `create_customer'
Completed 500 Internal Server Error in 22ms (ActiveRecord: 4.3ms | Allocations: 6384)


  
ActiveRecord::NotNullViolation (PG::NotNullViolation: ERROR:  null value in column "environment_id" violates not-null constraint
DETAIL:  Failing row contains (28, cvbcv, bcvbc, vbcvbcv, bcvbcvb, null, 2021-06-02 17:18:44.378509, 2021-06-02 17:18:44.378509).
):

我尝试了我能知道的一切,我三重检查了迁移和架构。我还检查了以前项目的活动记录 gem 版本,一切正常。

可能相关的一件事是,该项目在 Angular 中有一个前端。

标签: ruby-on-rails

解决方案


Rails 验证数据库中的环境存在,因为belongs_to它是客户。

您日志中的以下查询正在尝试使用以下内容加载环境ID=1

Environment Load (0.5ms)  SELECT "environments".* FROM "environments" WHERE "environments"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]

在此 Rails 尝试在INSERT没有环境参考的情况下尝试新客户,因为似乎 ID=1 的环境在数据库中不存在。


推荐阅读