控制器中的创建操作省略了引用字段



当我为环境创建客户时,我的rails应用程序出现问题,如果在insert语句中省略参数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中有一个前端。

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的环境。

多种解决方法。问题:environment_id是nil,并且已经在postgres DB中定义为not nil。

  • 您可以将required: true添加到关联中,然后该关联将检查是否存在。在您的情况下,会引发验证错误。

  • 你可以添加一个validates :enviroment_id, presence: true,它可以做同样的

  • 您将id合并到params 中

始终将environment_id合并到参数的示例

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

最新更新