【问题标题】:accessing children of children tables in Rails在 Rails 中访问子表的子表
【发布时间】:2009-04-21 12:41:40
【问题描述】:

在我正在编写的应用程序中,我的数据库具有“树状”结构,因此:

training has_many class_times 

class_time has_many reservations

有没有办法查看给定培训下的所有预订?当然,我可以遍历所有上课时间/添加外键,但由于某种原因,我脑后有个小声音说我可能不需要外键。

【问题讨论】:

    标签: ruby-on-rails ruby activerecord


    【解决方案1】:
    class Training < ActiveRecord::Base
      has_many :class_times
      has_many :reservations, :through => :class_times
    end
    
    class ClassTime < ActiveRecord::Base
      has_many :reservations
    end
    

    那么你可以这样做:

    training = Training.find(:first)
    training.reservations
    

    【讨论】:

    • 出于某种原因,我认为 :through 关联需要更多信息,大脑冻结了一秒钟。谢谢您的帮助!太棒了!
    猜你喜欢
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多