Ruby on Rails has_many:通过关联
示例
一个has_many:through协会经常被用来建立一个many-to-many与其他型号的连接。这种关联表明,通过进行第三个模型,可以将声明模型与另一个模型的零个或多个实例匹配。
例如,考虑一种医疗实践,患者会预约去看医生。相关的关联声明如下所示:
class Physician < ApplicationRecord has_many :appointments has_many :patients, through: :appointments end class Appointment < ApplicationRecord belongs_to :physician belongs_to :patient end class Patient < ApplicationRecord has_many :appointments has_many :physicians, through: :appointments end
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短