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
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_manyrelationships 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 => trueat the end of theadd_indexcommand.