Ruby on rails 在mongoid中为has_和_属于_-to _-many关系创建新表
我刚开始使用mongoid在rails中编写代码,以前我用sql、sqlite等编写代码,现在我对关联有点困惑。就像在sql中,当你想要一个has_和_属于_时,两个模型之间的许多关联都是这样做的 比如说Ruby on rails 在mongoid中为has_和_属于_-to _-many关系创建新表,ruby-on-rails,ruby,associations,mongoid,Ruby On Rails,Ruby,Associations,Mongoid,我刚开始使用mongoid在rails中编写代码,以前我用sql、sqlite等编写代码,现在我对关联有点困惑。就像在sql中,当你想要一个has_和_属于_时,两个模型之间的许多关联都是这样做的 比如说 class Student < ActiveRecord::Base has_and_belongs_to_many :subjects end class Subject < ActiveRecord::Base has_a
class Student < ActiveRecord::Base
has_and_belongs_to_many :subjects
end
class Subject < ActiveRecord::Base
has_and_belongs_to_many :students
end
在我们的迁移文件中,我们这样做
def change
create_table :students_subjects do |t|
t.references :student
t.references :subject
t.timestamps
end
结束
现在我的问题是,在使用mongoid或der时是否有必要创建一个新表是一种替代方法。请帮助我了解mongoid和rails。谢谢你,你只需要在类中包含一些代码,如下所示:
class Student
include Mongoid::Document
has_and_belongs_to_many :subjects
end
class Subject
include Mongoid::Document
has_and_belongs_to_many :students
end
这里有一个很棒的地方
希望它能帮助你
class Student
include Mongoid::Document
has_and_belongs_to_many :subjects
end
class Subject
include Mongoid::Document
has_and_belongs_to_many :students
end