rails用generate为两个模型创建has_and_belongs_to_many中间表

作者在 2015-06-30 09:57:14 发布以下内容

Where:

class Teacher < ActiveRecord::Base
  has_and_belongs_to_many :students
end

and

class Student < ActiveRecord::Base
  has_and_belongs_to_many :teachers
end

for rails 4:

rails generate migration CreateJoinTableStudentTeacher student teacher

for rails 3:

rails generate migration students_teachers student_id:integer teacher_id:integer

for rails < 3

script/generate migration students_teachers student_id:integer teacher_id:integer

(note the table name lists both join tables in alphabetical order)

and then for rails 3 and below only, you need to edit your generated migration so an id field is not created:

create_table :students_teachers, :id => false do |t| 


转自:http://stackoverflow.com/questions/4381154/rails-migration-for-has-and-belongs-to-many-join-table

Ruby | 阅读 5146 次
文章评论,共0条
游客请输入验证码
浏览2883797次
文章归档
最新评论
  • 时光拾荒者:CtrlCV还是强的😝
  • 硬识岩丝:解决了,太感谢了~
  • xiwang12:路过
  • 里苦不功:不校验mysql版本是否与当前django框架是否兼容