关联类型不匹配

时间:2016-06-14 17:36:42

标签: ruby ruby-on-rails-3

我正在尝试将用户详细信息添加到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)

创建控制器

 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

1 个答案:

答案 0 :(得分:1)

您的Authentication模型同时具有列和具有相同名称的关联:distributor。这是有问题的,因为尽管您尝试将名为distributor的列设置为"true" rails,但最终会尝试将关联设置为该值,这是导致异常的原因。

重命名列或关联,以便不再发生此冲突,问题就会消失。