简体   繁体   English

Rails - Polymorphic has_many:通过关联 - '无法在模型中找到源关联'错误

[英]Rails - Polymorphic has_many :through associations - 'Can't find source association in model' error

I have been scouring the internet and have tried lots of different ways to fix this problem but I am really stuck. 我一直在搜索互联网并尝试了很多不同的方法来解决这个问题,但我真的被卡住了。 I'm fairly new to rails so I might have missed something obvious! 我对rails很新,所以我可能错过了一些明显的东西!

The problem I have is with polymorphic associations involving 4 models: 我遇到的问题是涉及4个模型的多态关联:

(1) User, (2) Approver, (3) Recipient, (4) Note (1)用户,(2)审批人,(3)收件人,(4)注

A User has many Approvers and has many Recipients. 用户拥有许多审批者并且拥有许多收件人。 A user can also leave notes for both approvers and recipients. 用户还可以为审批者和收件人留下备注。 A note has a polymorphic association to approvers and recipients as :notable. 注释与批准者和收件人具有多态关联:值得注意。 My models look as follows: 我的模型如下:

Note.rb Note.rb

class Note < ApplicationRecord
  belongs_to :user
  belongs_to :notable, polymorphic: true
end

Approver.rb Approver.rb

 class Approver < ApplicationRecord
   belongs_to :user
   has_many :notes, as: :notable
 end

Recipient.rb Recipient.rb

class Recipient < ApplicationRecord
  belongs_to :user
  has_many :notes, as: :notable
end

User.rb User.rb

class User < ApplicationRecord
  has_many :approvers, dependent: :destroy
  has_many :recipients, dependent: :destroy

  # This is the bit that I think is the problem:
  has_many :notes, through: :approvers, source: :notable, source_type: "Note"
  has_many :notes, through: :recipients, source: :notable, source_type: "Note"
end

Basically I want to be able to do 基本上我希望能够做到

User.find(1).notes (...etc)

and show all of the notes for that user from both approvers and recipients. 并从审批者和收件人显示该用户的所有注释。

In the approver view, for instance, I can do @approver.notes.each and iterate through them fine. 例如,在审批者视图中,我可以执行@ approver.notes.each并对其进行详细迭代。

The error message that I am getting is: "Could not find the source association(s) :note_owner in model Recipient. Try 'has_many :notes, :through => :recipients, :source => '. Is it one of user or notes?" 我得到的错误消息是:“无法在模型收件人中找到源关联:note_owner。尝试'has_many:notes,:through =>:recipients,:source =>'。是用户还是笔记?”

Can anyone see what I am missing!? 任何人都能看到我错过的东西!?

In order to get the user's notes the way you mentioned, you will have to to add a foreign key for that user. 为了按照您提到的方式获取用户注释,您必须为该用户添加外键。 For example, 例如,

When an adviser adds a note, that note saves the adviser id and the note itself but there is currently no reference to the user that receives the note from the adviser. 当顾问添加注释时,该注释会保存顾问ID和注释本身,但目前没有提及接收顾问注释的用户。 If you add to the note schema a user id references, you can pull out all the notes of a particular user. 如果添加到用户ID引用的注释架构,则可以提取特定用户的所有注释。

EX. EX。

Note schema: 注意架构:

user_id: references (the user the note is for)
writer_id: references (the user that writes the note)
note: text (your actual note)

You can than build that note like this: 你可以像这样构建那个音符:

current_user.create_note(params[:user], params[:note])   # pass the user as a reference

def create_note(user, note)
  Note.create(user_id: user.id,
              write_id: current_user.id
              note: note)
end

Once you create a user like this, you can call on any user: user.notes and it should return an array of notes for that user. 创建这样的用户后,您可以调用任何用户:user.notes,它应该返回该用户的一系列注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM