2016-05-16 49 views
0

你好,我必須像模型一樣;模型中的一列到另一個模型

class CreateLecturers < ActiveRecord::Migration 
    def change 
    create_table :lecturers do |t| 
     t.string :firstname 
     t.string :lastname 
     t.string :position 
     t.string :email 

     t.timestamps null: false 
    end 
    end 
end 

這是我的第二個模型。

class CreateCurriculums < ActiveRecord::Migration 
    def change 
    create_table :curriculums do |t| 
     t.string :title 
     t.integer :hours 
     t.integer :year 
     t.text :description 

     t.timestamps null: false 
    end 
    end 
end 

我想遷移到課程講師。但不是與id,標題 它可能如何?

所以我使用rails-admin。當我添加一些課程,我想選擇下拉講師,當我添加一些講師,我想在模型之間選擇課程。

回答

0

無論如何,你必須有兩個模型之間的關聯。另外不要忘記添加curriculum_idlectures表。

curriculum.rb

has_many :lectures 

lecture.rb

belongs_to :curriculum 

要添加遷移

rails g migration add_curriculum_id_to_lectures curriculum_id:integer

相關問題