Pranavadurai Pranavadurai - 5 months ago 25
Ruby Question

Association Type mismatch

I am trying to add User details into 2 tables The table Associations are

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 for create

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


whenever i am trying to add the details i getting this error.. anyone tell me what i am doing wrong..

enter image description here

Answer

Your Authentication model has both a column and an association with the same name: distributor. This is problematic because all though you are trying to set the column named distributor to the "true" rails ends up trying to set the association to that value which is what causes the exception.

Rename either the column or the association so that you no longer have this clash and the problem should go away.