将所有内容放在关系模型中或添加新模型

时间:2011-04-22 09:31:29

标签: ruby-on-rails ruby-on-rails-3 entity-relationship

我遵循了Michael Hartle的书Rails Tutorial并让用户关注系统,该系统通过关系表工作,follower_idfollowed_id

我想添加另一种关系,这次是一个收藏系统。我是否最好将列添加到关系表并使用它,还是应该创建一个新模型以保持优先关系?

2 个答案:

答案 0 :(得分:1)

我认为你的问题没有明确答案。

但是为了简单起见,我会考虑只使用一个带有标志的Connection

  • is_followed
  • is_favorite

特别是如果你只喜欢跟随的人,那么验证变得容易多了。仍然允许模型中的简单访问者

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 => ...

答案 1 :(得分:-1)

顺便说一下你可以声明的外键关系是你必须在喜欢的迁移文件中写t.reference:user如果你想要用户外键作为收藏表中的列