Putting everything in the relationships model or adding a new model
I followed the Michael Hartle book Rails Tutorial and made 开发者_如何学Pythona 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?
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 => ...
and by the way the foreign key relation you can declare is the u have to write t.reference:user in the favourite migration file if u want user foreign key as column in the favourite table
精彩评论