[英]Rails polymorphic association accepts_nested_attributes
I currently a polymorphic association set up like this 我目前是这样设置的多态关联
class Reading < ApplicationRecord
has_one :audio, as: :audioable
accepts_nested_attributes_for :audio
end
class Audio < ApplicationRecord
belongs_to :audioable, polymorphic: true
end
In my RSPEC test, I have this set up 在我的RSPEC测试中,我有这个设置
@reading = attributes_for(:reading, creator: @user, body: "who let the dogs out? Must be me\n\nMeMeMe")
@reading[:audio_attributes] = attributes_for(:audio)
However, I get the following errors when posting this @reading
to the reading#create
controller 但是,将此@reading
发布到reading#create
@reading
时出现以下错误
{:errors=>{:"audio.audioable"=>["must exist"]}}
For non-polymorphic associations I've gotten it to create both the parent and nested resources using inverse_of
but I'm not sure how to do it for polymorphic associations. 对于非多态关联,我已经使用inverse_of
创建了父资源和嵌套资源,但我不知道如何为多态关联做这件事。
inverse_of
不能用于多态关联,你看过Single Table Inheritance它可能是一个很好的解决方案,取决于你想要实现的目标
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.