has-and-belongs-to-many

Rails has_and_belongs_to_many migration

被刻印的时光 ゝ 提交于 2019-11-26 22:29:03
问题 I have two models restaurant and user that I want to perform a has_and_belongs_to_many relationship. I have already gone into the model files and added the has_and_belongs_to_many :restaurants and has_and_belongs_to_many :users I assume at this point I should be able to do something like with Rails 3: rails generate migration .... but everything I have tried seems to fail. I'm sure this is something really simple I'm new to rails so I'm still learning. 回答1: You need to add a separate join

Rails migration for has_and_belongs_to_many join table

只愿长相守 提交于 2019-11-26 10:08:01
问题 How do I do a script/generate migration to create a join table for a has_and_belongs_to_many relationship? The application runs on Rails 2.3.2, but I also have Rails 3.0.3 installed. 回答1: 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