【发布时间】:2015-07-21 14:51:47
【问题描述】:
我想加入具有多态多对多关联的两个模型。
我的桌子是父母和孩子,可以互相成为朋友。为此,我想创建一个朋友关联表,例如父母或孩子可以与其他父母或孩子成为朋友
我阅读了一些教程,涵盖了 has_many、has_many through 和多态关联,但还没有任何内容可以将这两个功能混合在一起。
我尝试了以下方法:
朋友桌
t.integer :me
t.string :me_type
t.integer :him
t.string :him_type
儿童模型
class Kid < ActiveRecord::Base
belongs_to :parent
has_many :friends, as: :me, polymorphic: true
belongs_to :friends, as: :you, polymorphic:true
父模型
class Parent < ActiveRecord::Base
has_many :friends, as: :me, polymorphic: true
belongs_to :friends, as: :you, polymorphic:true
但是,我对如何定义朋友模型感到困惑。 关于如何在 rails 中定义这种关系的任何提示?
【问题讨论】:
标签: ruby-on-rails activerecord parametric-polymorphism