Rails two has_many

时间:2012-08-11 12:32:28

标签: ruby-on-rails ruby-on-rails-3 activerecord

有两种不同类型的用户:摄影师和寻找摄影师的人(尽管都是用户)。普通用户可以选择一组摄影师并向该组发布“简报”。什么是最好的模型关联设置?

Brief
  belongs_to :user # a user looking for a photographer
  has_many :photographers, :class_name => "User", :through => :jobs # photographers who have been selected to participate in this brief

User
  has_many :briefs
  has_many :jobs, :class_name => "Brief", :through => :jobs

1 个答案:

答案 0 :(得分:0)

查找用户/摄影师/客户的单表继承。

顺便说一句,“has_many:jobs,:through =>:jobs”对你没有帮助。

我想你想要这个:

Class Person < User
  has_many :briefs, :foreign_key => "poster_id"
end

Class Photographer < User
 has_many :briefs, :foreign_key => "photographer_id"
end