关于has_many的问题:通过关联

时间:2018-07-05 23:07:21

标签: dependencies ruby-on-rails-5 associations has-many-through

我有一个用户和一个作业模型(用户has_many个作业和Job About_to User)。用户有两个account_type(学生和合作伙伴)。合作伙伴用户可以通过该帐户发布/创建工作;学生应该能够与工作相匹配,也可以申请工作。

我不知道为用户和工作建立独立的关系是否更好,因为它们与匹配项和应用程序相关,或者通过关系实现has_many。以下是我正在考虑的两条路线-您能否就更好的路线提出一些建议/建议(例如,这将更易于实施,长期维护中的有利条件)?

有很多/通过关系

Job
belongs_to :user                        => logic: partner users create jobs
has_many :matches :through :users       => logic: partner users (who posted jobs) should see students/potential candidates (to invite them to apply)
has_many :applications through :matches => logic: only match students can see/apply to jobs


Match
belongs_to :job                          => logic: see above (for partner)
belongs_to :user                         => logic: see above (for student)

Application
belongs_to :job                          => logic: same as match
belongs_to :user                         => logic: same as match

User
has_many :jobs                           => logic: partner user can post jobs
has_many :applications :through :matches => logic: student can submit applications only to match opportunities 
has_many :matches :through :jobs         => logic: student can see jobs the qualify for

独立关系

Job
belongs_to :user      
has_many :matches
has_many :applications


Match
belongs_to :job       
belongs_to :user      

Application
belongs_to :job        
belongs_to :user       

User
has_many :jobs
has_many :applications
has_many :matches

我不清楚通过阅读has_many:文档来了解这些关系是否旨在显示依赖关系,在这种情况下,这意味着没有匹配项就无法存在应用程序,而没有工作或用户就不会存在匹配项,或如果这对我来说是一种误解(我知道dependency:destroy是模型之间关系的一个单独选项)。欢迎您对所提出的两条路线以及has_many:through关系的解释提供一些反馈。

0 个答案:

没有答案