简体   繁体   中英

Rails two has_many

There are two different types of Users: photographers and people who are looking for a photographer (both Users though). Normal users can choose a group of photographers and post "briefs" to that group. What's the best model association setup?

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

Look up Single Table Inheritance for the users / photographers / clients.

Btw, "has_many :jobs, :through => :jobs" isn't going to help you.

I think you want this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM