反向多态关联

Jar*_*own 5 polymorphism ruby-on-rails associations

我有一个父对象Post,它有以下子对象.

has_one :link
has_one :picture
has_one :code
Run Code Online (Sandbox Code Playgroud)

这些孩子是互相排斥的.

有没有办法反向使用多态关联,以便我不必在我的Post表中有link_id,picture_id和code_id字段?

Ron*_*nze 8

我写了一个小小的Gist,展示了如何做到这一点:https: //gist.github.com/1242485