Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails ActiveRecord::AssociationTypeMismatch_Ruby On Rails_Ruby - Fatal编程技术网

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