Я пытаюсь создать notification
, чтобы добавить user friend
.Однако, когда я пытаюсь добавить друга к ошибке.
Модель дружбы
class Friendship < ApplicationRecord
after_save :notification
belongs_to :user
belongs_to :friend, class_name: "User"
has_many :notification
private
def all_friendships
@all_friend = Friendship.order('created_at DESC').limit(1)
end
def notification
all_friendships.each do |friend|
Notification.create(recipient: friend.friend_id, actor: self.user, action: "add friend", notifiable: friend)
end
end
end
Контроллер дружбы
class FriendshipsController < ApplicationController
before_action :set_friendships, only: [:show, :update, :destroy]
def index; end
def create
@friendship = current_user.friendships.build(friend_id: params[:friend_id],
accepted: false)
if @friendship.save
redirect_to current_user
else
redirect_to users_path
end
end
def update
@friendship.update_attributes(accepted: true)
if @friendship.save
redirect_to current_user
else
redirect_to users_path
end
end
def destroy
@friendship.destroy
redirect_to current_user
end
private
def set_friendships
@friendship = Friendship.where(user_id: params[:id]).or(Friendship.where(friend_id: params[:id])).first
end
end
Модель уведомления
class Notification < ApplicationRecord
belongs_to :recipient, class_name: "User"
belongs_to :actor, class_name: "User"
belongs_to :notifiable, polymorphic: true
scope :unread, -> { where(read_at: nil) }
end
Ошибка
ActiveRecord::AssociationTypeMismatch in FriendshipsController#create
User(#70140147575580) expected, got 2 which is an instance of Integer(#70140150807860)
Extracted source (around line #15):
def notification
all_friendships.each do |friend|
Notification.create(recipient: friend.friend_id, actor: self.user, action: "add friend", notifiable: friend)
end
end
end