Ruby on rails Rails ActiveRecord::AssociationTypeMismatch
我正在尝试创建Ruby on rails Rails ActiveRecord::AssociationTypeMismatch,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在尝试创建通知以添加用户朋友。 但是,当我尝试向错误中添加朋友时 友谊模式 class Friendship < ApplicationRecord after_save :notification belongs_to :user belongs_to :friend, class_name: "User" has_many :notification private def all_friendships @all_friend = 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 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
您可以设置
朋友id
(整数),而不是用户模型(收件人:friend.frien\u id)。需要设置用户模型。尝试:
def notification
all_friendships.each do |friend|
Notification.create(recipient: friend, actor: self.user, action: "add friend", notifiable: friend)
end
end
更新:调用友谊
类朋友
的变量是不好的,因为朋友
应该是用户
。
无论如何,代码如下:
def notification
all_friendships.each do |friend|
Notification.create(recipient: friend.friend, actor: self.user, action: "add friend", notifiable: friend)
end
end
无论如何,一个错误用户(#70140183755940)更新了响应
def notification
all_friendships.each do |friend|
Notification.create(recipient: friend, actor: self.user, action: "add friend", notifiable: friend)
end
end
def notification
all_friendships.each do |friend|
Notification.create(recipient: friend.friend, actor: self.user, action: "add friend", notifiable: friend)
end
end