2015-09-24 54 views
1

我有3個表,學生消息和教練。 現在我想創建一個連接遷移表MESSAGE_ID coach_id和student_id數據如何在ruby上創建3表連接遷移on rails

請幫助創建遷移查詢

任何幫助,高度讚賞

得到這個錯誤,當我試圖下面的代碼

== 20150924072052 AssociationTable: migrating 
=================================             
-- create_table(:associations) 
rake aborted! 
StandardError: An error has occurred, all later migrations canceled: 

Mysql2::Error: Key column 'student_id' doesn't exist in table: ALTER TABLE `associations` ADD CONSTRAINT `fk_rails_122f0db022` FOREIGN KEY (`student_id`) REFERENCES `student` (`id`)` 
+1

可能重複[生成遷移 - 創建連接表(http://stackoverflow.com/questions/17765249/generate-migration-create-join -table) – Pavan

+0

@Pavan這是2表連接遷移,但我需要3表連接,這是不工作的情況下,三個 – SreRoR

回答

2

這將是這樣的:

$ rails g migration AssociationTable 

...這將創建一個文件,如下列:

#db/migrate/association_table_[timestamp].rb 
class AssociationTable < ActiveRecord::Migration 
    def change 
    create_table :associations do |t| 
     t.references :student 
     t.references :message 
     t.references :coache 
     t.timestamps null: false 
    end 
    end 
end 

這將創建一個表具有以下欄目:

id 
student_id 
message_id 
coach_id 
created_at 
updated_at 

這將在has_many :through關係中使用,這需要你有一個join model

#app/models/association.rb 
class Association < ActiveRecord::Base 
    belongs_to :student 
    belongs_to :message 
    belongs_to :coach 
end 

-

要更新您關於has_many :throughhas_and_belongs_to_many之間的選擇,這裏是你需要知道:

兩者之間的主要區別在於:has_many :through利用join model的事實。連接模型基本上是一個模型,ActiveRecord將通過該模型填充依賴關聯數據。總之,它將兩個模型「結合」在一起。

雖然加盟模式是HABTMHMT之間最大的區別,還有一個技術原因,爲什麼你會在它們之間選擇 - HMT允許你有在加盟模式額外的屬性。

這是這樣的事情很重要:

#app/models/doctor.rb 
class Doctor < ActiveRecord::Base 
    has_many :appointments 
    has_many :patients, through: :appointments 
end 

#app/models/appointment.rb 
class Appointment < ActiveRecord::Base 
    #columns id | doctor_id | patient_id | time | created_at | updated_at 
    belongs_to :doctor 
    belongs_to :patient 
end 

#app/models/patient.rb 
class Patient < ActiveRecord::Base 
    has_many :appointments 
    has_many :doctors, through: :appointments 
end 

的連接模型(預約),因此將能夠有你就可以與其他車型使用的具體數據:

@doctor = Doctor.find 1 
@appointments = @doctor.appointments.where(time: ____) 
@patients = @appointments.patients 

很多疑問。我希望你明白這個主意。

enter image description here

-

has_and_belongs_to_many是簡單了很多,但我不知道它是否適合3個表(不明白爲什麼它不應該)。

enter image description here

這消除了對加盟模式的必要性,並在此過程中阻止您能夠在協會使用額外的屬性(注意如何加入沒有idtimestamp屬性?) 。對於HABTM表的命名約定albabetical_plurals - 你的情況recipient_messages

同樣,我不知道這是否會爲3路連接工作,但這裏是你會怎麼做:

#app/models/student.rb 
class Student < ActiveRecord::Base 
    has_and_belongs_to_many :messages, join_table: "recipients_messages", foreign_key: "recipient_id", association_foreign_key: "message_id" 
end 

#app/models/message.rb 
class Message < ActiveRecord::Base 
    has_and_belongs_to_many :recipients, join_table: "recipients_messages", foreign_key: "message_id", association_foreign_key: "recipient_id" 
end 

想想你的要求具體,我會說你會更好has_many :through

原因是如果您發送的是消息,您需要一種方法來了解消息的發送對象,消息內容以及消息是否已被讀取。

我會用messages作爲連接型號:

#app/models/message.rb 
class Message < ActiveRecord::Base 
    #columns id | student_id | coach_id | message | read | created_at | updated_at 
    belongs_to :student 
    belongs_to :coach 
end 

#app/models/coach.rb 
class Coach < ActiveRecord::Base 
    has_many :messages 
end 

#app/models/student.rb 
class Student < ActiveRecord::Base 
    has_many :messages 
end 
+0

顯示此錯誤'$ rake db:migrate == 20150924072052 AssociationTable:migrating ===== ============================ - create_table(:associations) rake中止! StandardError:發生錯誤,所有後來的遷移取消: Mysql2 ::錯誤:表'關聯'已經存在:CREATE TABLE'associations'('id' int(11)auto_increment PRIMARY KEY,'created_at' datetime NOT NULL ''updated_at' datetime NOT NULL)ENGINE = InnoDB/home/vagrant/.rvm/gems/ruby​​-2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:305: '' – SreRoR

+0

你之前跑過遷移嗎?你已經有一個名爲'associations'的表格 –

+0

是的,我會回滾並且會檢查 – SreRoR