开发者

rails polymorphic model implementation

In my project, I have a relationship model that allow users to follow each other.

开发者_C百科
class Relationship < ActiveRecord::Base
  attr_accessible :followed_id

  belongs_to :follower, :class_name => "User"
  belongs_to :followed, :class_name => "User"

end

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.


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.

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]).

I would also make the relation UNIQUE, which you can do appending :unique => true at the end of the add_index command.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜