I have two models that have a has_many association to the same object. I have a User, an Admin, and Visits.
Users has_many Visits Admins has_many Visits
Every time I create a Visit with a User it works, but when I do it with an Admin it gives me an error ActiveRecord::AssociationTypeMismatch.
The full error is this:User(#70282715553200) expected, got # which is an instance of Admin(#70282709528720)
def create
@visit = @service.visits.new(visit_params)
if user_signed_in?
@visit.user = current_user
else
@visit.user = current_admin
end
if @visit.save
redirect_to service_visits_path(@service)
else
redirect_to @services
end
end
class Admin < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and
:omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :visits, dependent: :destroy
end
==============================
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and
:omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :visits, dependent: :destroy
end
==============================
class Visit < ApplicationRecord
belongs_to :user
belongs_to :admin
belongs_to :service
end
Aucun commentaire:
Enregistrer un commentaire