【发布时间】:2017-08-16 06:50:24
【问题描述】:
我刚开始使用 mongoid 在 Rails 中编写代码,之前我使用 sql、sqlite 等编写代码,现在我对关联感到有些困惑。就像在 sql 中,当你想要以这种方式在两个模型之间建立 has_and_belongs_to_many 关联时
p>例如
class Student < ActiveRecord::Base
has_and_belongs_to_many :subjects
end
class Subject < ActiveRecord::Base
has_and_belongs_to_many :students
end
我们创建一个新表
rails g migration CreateJoinTableStudentSubject student subject
在我们的迁移文件中我们这样做
def change
create_table :students_subjects do |t|
t.references :student
t.references :subject
t.timestamps
end
结束
现在我的问题是在使用 mongoid 时创建一个新表是必要的,或者 der 是执行此操作的另一种方法。请帮助我是 mongoid 和 rails 的新手。谢谢
【问题讨论】:
标签: ruby-on-rails ruby associations mongoid