Rails - 多态 has_many:通过关联 - “无法在模型中找到源关联”错误
Rails - Polymorphic has_many :through associations - 'Can't find source association in model' error
我一直在网上搜索并尝试了很多不同的方法来解决这个问题,但我真的被困住了。我是 rails 的新手,所以我可能错过了一些明显的东西!
我遇到的问题是涉及 4 个模型的多态关联:
(1) 用户,(2) 批准人,(3) 收件人,(4) 备注
一个用户有很多批准人和很多收件人。用户还可以为批准人和收件人留下注释。注释与批准者和接收者具有多态关联,如:显着。我的模型如下所示:
Note.rb
class Note < ApplicationRecord
belongs_to :user
belongs_to :notable, polymorphic: true
end
Approver.rb
class Approver < ApplicationRecord
belongs_to :user
has_many :notes, as: :notable
end
Recipient.rb
class Recipient < ApplicationRecord
belongs_to :user
has_many :notes, as: :notable
end
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
基本上我想能做到
User.find(1).notes (...etc)
并显示批准人和收件人对该用户的所有注释。
例如,在批准者视图中,我可以执行 @approver.notes.each 并很好地遍历它们。
我收到的错误消息是:"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?"
谁能看到我错过了什么!?
为了按照您提到的方式获取用户的注释,您必须为该用户添加一个外键。例如,
当顾问添加注释时,该注释会保存顾问 ID 和注释本身,但目前没有对从顾问那里收到注释的用户的引用。如果将用户 ID 引用添加到笔记模式,则可以提取特定用户的所有笔记。
EX.
笔记架构:
user_id: references (the user the note is for)
writer_id: references (the user that writes the note)
note: text (your actual note)
你可以像这样构建笔记:
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
像这样创建用户后,您可以调用任何用户:user.notes 它应该 return 该用户的注释数组。
我一直在网上搜索并尝试了很多不同的方法来解决这个问题,但我真的被困住了。我是 rails 的新手,所以我可能错过了一些明显的东西!
我遇到的问题是涉及 4 个模型的多态关联:
(1) 用户,(2) 批准人,(3) 收件人,(4) 备注
一个用户有很多批准人和很多收件人。用户还可以为批准人和收件人留下注释。注释与批准者和接收者具有多态关联,如:显着。我的模型如下所示:
Note.rb
class Note < ApplicationRecord
belongs_to :user
belongs_to :notable, polymorphic: true
end
Approver.rb
class Approver < ApplicationRecord
belongs_to :user
has_many :notes, as: :notable
end
Recipient.rb
class Recipient < ApplicationRecord
belongs_to :user
has_many :notes, as: :notable
end
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
基本上我想能做到
User.find(1).notes (...etc)
并显示批准人和收件人对该用户的所有注释。
例如,在批准者视图中,我可以执行 @approver.notes.each 并很好地遍历它们。
我收到的错误消息是:"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?"
谁能看到我错过了什么!?
为了按照您提到的方式获取用户的注释,您必须为该用户添加一个外键。例如,
当顾问添加注释时,该注释会保存顾问 ID 和注释本身,但目前没有对从顾问那里收到注释的用户的引用。如果将用户 ID 引用添加到笔记模式,则可以提取特定用户的所有笔记。
EX.
笔记架构:
user_id: references (the user the note is for)
writer_id: references (the user that writes the note)
note: text (your actual note)
你可以像这样构建笔记:
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
像这样创建用户后,您可以调用任何用户:user.notes 它应该 return 该用户的注释数组。