向连接表添加额外数据 - Rails [英] Add extra data to the join table - Rails

查看:38
本文介绍了向连接表添加额外数据 - Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,具有年模型和课程模型.目前有一个 has_and_belongs_to_many 关系将这些与 course_years 表联系起来,但是我想在 course_years 表中存储一个额外的字段.

I am developing an application, with a years model and a courses model. Currently there is a has_and_belongs_to_many relationship linking these with a courses_years table, however I would like to store an extra field in the courses_years table.

新字段是一个名为compulsory"的布尔值.

The new field is a boolean value called "compulsory".

有没有简单或好的方法来做到这一点?

Is there an easy or nice way of doing this?

推荐答案

切换到使用 :has_many =>:through 关联,专为需要连接模型而设计.ActiveRecord Associations Rails Guide 中有更多详细信息.

Switch to using a :has_many => :through association, which is specifically designed for when you need a join model. There are more details in the ActiveRecord Associations Rails Guide.

这篇关于向连接表添加额外数据 - Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆