gpt4 book ai didi

ruby - 关联类型不匹配

转载 作者:太空宇宙 更新时间:2023-11-03 16:19:03 25 4
gpt4 key购买 nike

我正在尝试将用户详细信息添加到 2 个表中表关联是

Distributor Table:

class Distributor < ApplicationRecord

has_one :authentication, dependent: :destroy
end

Authentication Table

class Authentication < ApplicationRecord

belongs_to :distributor
belongs_to :user

validates :email,presence: true



before_save :create_remember_token

private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end

end

Authentication table details


Authentication(id: integer, email: string, password: string, created_at: datetime, updated_at: datetime, admin: boolean, remember_token: string, user_id: integer, mail_confirmation: boolean, distributor: boolean, distributor_id: integer)

创建 Controller

 def create
@distributor = Distributor.new(distributor_params)
if @distributor.save
params[:distributor][:distributor_id] = @distributor.id
params[:distributor][:password] = "Default"
params[:distributor][:distributor] = "true"
@authe_distributor = Authentication.new(authen_distributor_params)
if @authe_distributor.save
redirect_to @distributor
else
render 'new'
end
else
render 'new'
end

end

每当我尝试添加详细信息时,我都会收到此错误。任何人都可以告诉我我做错了什么。

enter image description here

最佳答案

您的Authentication 模型有一个列和一个同名的关联:distributor。这是有问题的,因为尽管您尝试将名为 distributor 的列设置为 "true" rails 最终还是尝试将关联设置为导致异常的那个值。

重命名列或关联,这样您就不会再遇到此冲突,问题就会消失。

关于ruby - 关联类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37818673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com