轨道多态模型实现 [英] rails polymorphic model implementation
问题描述
在我的项目,我有一个关系模式,允许用户按照对方。
In my project, I have a relationship model that allow users to follow each other.
class Relationship < ActiveRecord::Base
attr_accessible :followed_id
belongs_to :follower, :class_name => "User"
belongs_to :followed, :class_name => "User"
end
现在,我想还允许用户按照课程和团体。难道我开始一个新的followedCourse和followedGroup模型或让我的关系模型的多态?我怎么做?谢谢。
Now, I want to also allow users to follow courses and groups. Do I start a new followedCourse and followedGroup model or do I make the relationship model polymorphic? How do I do that? Thanks.
推荐答案
我不会用多态的可能,大表。我认为最好的方法是使用 has_and_belongs_to_many
关系的这种关系。
I wouldn't use polymorphic for potentially-large tables. I think the best way to go is to use has_and_belongs_to_many
relationships for this kind of relation.
记住要创建索引的USER_ID,GROUP_ID的东西加快一点。你可以通过使用 add_index(:TABLE_NAME,[:USER_ID,:GROUP_ID])
。
Remember to create indexes on user_id, group_id to speed up things a bit. You can do that by using add_index(:table_name,[:user_id,:group_id])
.
我也做了独特的关系,你可以不追加:唯一=&GT;真正的
在 add_index
命令结束。
I would also make the relation UNIQUE, which you can do appending :unique => true
at the end of the add_index
command.
这篇关于轨道多态模型实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!