0

我很难找出最好的方法来做到这一点。我有一个 User 模型和一个 Tournament 模型,并在这两个模型之间建立了一个 has_many :through 关系,称为“followed_tournaments”,以便用户可以关注锦标赛。因此,我已经在 User 模型中有一个 has_many :tournaments ,在 Tournament 模型中有一个 has_many :users ,这样一个锦标赛就有很多追随者,一个用户可以关注很多锦标赛。

我想建立另一个 habtm 或 has_many :through 关系,以便用户可以被视为锦标赛的“贡献者”——与我已经建立的关系完全不同。我希望锦标赛有任意数量的贡献者,并且用户可以为许多锦标赛做出贡献。

执行此操作的最佳方法是什么?

4

1 回答 1

0

使用sourceclass_name

class Tournament < ActiveRecord::Base
  has_many :users # ... whatever

  has_many :contributions

  # using class_name
  has_many :contributors, :through => :contributions

  # using source
  has_many :contributors, :through => :contributions, :source => :user
end

class Contribution < ActiveRecord::Base
  belongs_to :tournament

  # using class_name
  belongs_to :contributor, :class_name => 'User'

  # using source
  belongs_to :user
end
于 2012-08-09T05:15:53.753 回答