2016-07-07 134 views
0

我目前多態關聯設置這樣Rails的多態關聯accepts_nested_attributes

class Reading < ApplicationRecord 
    has_one :audio, as: :audioable 
    accepts_nested_attributes_for :audio 
end 

class Audio < ApplicationRecord 
    belongs_to :audioable, polymorphic: true 
end 

在我RSPEC測試中,我有這樣的設置

@reading = attributes_for(:reading, creator: @user, body: "who let the dogs out? Must be me\n\nMeMeMe") 
@reading[:audio_attributes] = attributes_for(:audio) 

不過,我發佈這個時候遇到下面的錯誤@readingreading#create控制器

{:errors=>{:"audio.audioable"=>["must exist"]}} 

For非多態協會我已經得到它使用inverse_of創建父和嵌套資源,但我不知道如何做多態關聯。

回答

0

inverse_of不能用於多態關聯,有你看着Single Table Inheritance這可能是取決於你正在努力實現

什麼好的解決辦法