Search code examples
ruby-on-railsruby-on-rails-3entity-relationship

Putting everything in the relationships model or adding a new model


I followed the Michael Hartle book Rails Tutorial and made a user following system that works through a relationships table, with a follower_id and a followed_id.

I want to add another relationship, this time a favoriting system. Would I be best to add the column to the relationships table and use that or should I create a new model to hold the favoriting relationship?


Solution

  • I don't think there is a definite answer to your question.

    But to keep things simple I would consider to use only one Connection table with flags

    • is_followed
    • is_favorite

    Especially if you can only favorite followed people, validation becomes a lot easier. Still allows easy accessors in your model

    class Person < ActiveRecord::Base
    
      ...
    
      has_many :favorites, :through => :connections, :conditions => { :is_favorite => true }, :source => ...
      has_many :followers, :through => :connections, :conditions => { :is_followed => true }, :source => ...
      has_many :followee,  :through => :connections, :conditions => { :is_followed => true }, :source => ...