[英]Polymorphic, has_many_and_belongs_to_many self referential association
I want to join two models with a polymorphic many to many association. 我想将两个具有多态多对多关联的模型加入其中。
My table are Parents and Kids, and can become friends with each other. 我的桌子是父母和孩子,可以彼此成为朋友。 To achieve this, I want to create a Friend association table, such as Parents or Kids can become friends with other Parents or Kids 为此,我想创建一个“朋友”关联表,例如“父母或孩子”可以与其他“父母或孩子”成为朋友
I read a few tutorial, covering the has_many, has_many through, and polymorphic associations, but nothing yet that could mix the two features together. 我读了一些教程,涵盖了has_many,has_many through和多态关联,但是还没有什么可以将这两个功能混合在一起的。
I tried the following: 我尝试了以下方法:
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
However, I'm stuck on how to define friend model. 但是,我坚持如何定义朋友模型。 Any hint on how to define this relationship in rails ? 关于如何在rails中定义这种关系的任何提示?
Try next associations, 尝试下一个关联,
Kid model 孩子模型
class Kid < ActiveRecord::Base
belongs_to :parent
has_many :my_friends,
:as => :me,
:class_name => "Friend"
has_many :their_friends,
:as => :you,
:class_name => "Friend"
end
Parent model 父模型
class Parent < ActiveRecord::Base
has_many :my_friends,
:as => :me,
:class_name => "Friend"
has_many :their_friends,
:as => :you,
:class_name => "Friend"
end
Friend model 朋友模型
class Friend < ActiveRecord::Base
belongs_to :me,
:polymorphic => true
belongs_to :you,
:polymorphic => true
end
Another approach 另一种方法
class Friend < ActiveRecord::Base
belongs_to :friendable, :polymorphic => true
belongs_to :parent
belongs_to :kid
belongs_to :...
end
Then, in each friend type (parent, child, cousin, etc.) model, add the relationship. 然后,在每个朋友类型(父母,孩子,堂兄等)模型中,添加关系。 For example, in your parent model 例如,在您的父模型中
# DB setup connection between parent and friends,
# Friend is polymorphic so id and type must match,
has_many :passive_friends, class_name: "Friend",
foreign_key: "friend_id",
dependent: :destroy
# Controller setup for DB access through model
has_many :friends, through: :passive_friends,
source: :friendable,
source_type: 'Parent'
And in your kid model 在你的孩子模型中
# DB setup connection between kids and friends,
# Friend is polymorphic so id and type must match,
has_many :passive_friends, class_name: "Friend",
foreign_key: "friend_id",
dependent: :destroy
# Controller setup for DB access through model
has_many :friends, through: :passive_friends,
source: :friendable,
source_type: 'Kid'
Then you can do things like 然后你可以做类似的事情
Mr. Smith has [<%= parent.friends.count.to_s %>] friends.
And it will include all friends of all types in the total. 它将包括所有类型的所有朋友。
(You may not want the dependent destroy param, but you should delete the friend record when the relationship is deleted.) (您可能不希望从属销毁参数,但是在删除关系时,您应该删除好友记录。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.