我有三张桌子,学生留言和教练。现在,我想用message_id、coach_id和student_id创建一个连接迁移表。
请帮助创建迁移查询。
任何帮助都是非常感谢的。
当我尝试下面的代码时,得到了这个错误
== 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`)`发布于 2015-09-24 06:55:15
会是这样的:
$ 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 :through和has_and_belongs_to_many之间的选择,以下是您需要了解的信息:
两者的主要区别在于,has_many :through使用了一个http://guides.rubyonrails.org/association_basics.html#choosing-between-has-many-through-and-has-and-belongs-to-many。连接模型基本上是一个模型,ActiveRecord将通过该模型填充依赖的关联数据。简而言之,它将两个模型“连接”在一起。
虽然连接模型是HABTM和HMT之间的巨大区别,但您要在两者之间选择的另一个技术原因是,HABTM允许您在连接模型中拥有额外的属性。
对于这样的事情来说,这是很重要的:
#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因此,join模型(约会)将能够拥有与其他模型一起使用的特定数据:
@doctor = Doctor.find 1
@appointments = @doctor.appointments.where(time: ____)
@patients = @appointments.patients很多问题。我希望你明白这个想法。

--
has_and_belongs_to_many要简单得多,尽管我不确定它是否适用于3个表(不知道为什么不应该)。

这就消除了对联接模型的需求,并且在这个过程中,您无法在关联中使用额外的属性(注意join表是如何没有id或timestamp属性的?)HABTM表的命名约定是albabetical_plurals (在您的例子中是albabetical_plurals)。
再说一遍,我不知道这是否适用于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
endhttps://stackoverflow.com/questions/32754973
复制相似问题