在 mongoid 中为 has_and_belongs_to_many 关系创建一个新的 table
Creating a new table in mongoid for has_and_belongs_to_many relationship
我刚开始使用 mongoid 在 rails 中编码,之前我在 sql、sqlite 等中编码,现在我对 associations.like 在 sql 当你想要 has_and_belongs_to_many 两个模型之间的关联时,你可以这样做
例如
class Student < ActiveRecord::Base
has_and_belongs_to_many :subjects
end
class Subject < ActiveRecord::Base
has_and_belongs_to_many :students
end
然后我们创建一个新的 table 作为
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 时是否需要创建一个新的 table 是另一种方法 this.plz help Iam new to mongoid and rails.thank you
您只需要像这样在 类 中包含一些代码:
class Student
include Mongoid::Document
has_and_belongs_to_many :subjects
end
class Subject
include Mongoid::Document
has_and_belongs_to_many :students
end
这里有一个很棒的documentation。
希望对您有所帮助!
我刚开始使用 mongoid 在 rails 中编码,之前我在 sql、sqlite 等中编码,现在我对 associations.like 在 sql 当你想要 has_and_belongs_to_many 两个模型之间的关联时,你可以这样做
例如
class Student < ActiveRecord::Base
has_and_belongs_to_many :subjects
end
class Subject < ActiveRecord::Base
has_and_belongs_to_many :students
end
然后我们创建一个新的 table 作为
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 时是否需要创建一个新的 table 是另一种方法 this.plz help Iam new to mongoid and rails.thank you
您只需要像这样在 类 中包含一些代码:
class Student
include Mongoid::Document
has_and_belongs_to_many :subjects
end
class Subject
include Mongoid::Document
has_and_belongs_to_many :students
end
这里有一个很棒的documentation。
希望对您有所帮助!