【问题标题】:Defining Relationship ( Many to Many) for 3 Models in Rails在 Rails 中为 3 个模型定义关系(多对多)
【发布时间】:2013-03-26 07:24:30
【问题描述】:

我在 Rails 中有三个模型:课程、成绩和主题。

关系场景是这样的:

'G1' 年级的课程'C1' has_many Topics (t1,t2,t3)

'G2' 年级的课程'C2' has_many Topics (t1,t2,t4,t5) [ A 主题可以在许多不同的课程中,但可能在不同的课程中教授 成绩]

G1 级本身可以是 C1、C2 等多个课程的一部分

我需要存储信息,以便对于主题 t1,我可以说:

G1 年级 C1 授课

G2 年级 C2 授课

如何在 Rails 中进行设置?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 database-design activerecord relationship


    【解决方案1】:

    我相信这似乎是您所追求的模型?

    class Curriculum < ActiveRecord::Base
      has_many :grades
    end
    
    class Grade < ActiveRecord::Base
      belongs_to :curriculum
      has_and_belongs_to_many :topics
    class
    
    class Topic < ActiveRecord::Base
      has_and_belongs_to_many :grades
    end
    

    【讨论】:

    • 也许这是有道理的。课程A级1主题1主题2课程B级2主题1主题3
    猜你喜欢
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 2016-12-10
    • 1970-01-01
    • 2018-12-12
    • 2018-03-28
    • 2021-05-25
    • 2016-03-06
    相关资源
    最近更新 更多