如何在表和类名不同时定义has_many:through关系

时间:2017-09-20 07:32:11

标签: ruby-on-rails activerecord

我想知道当模型的表名和类名彼此不同时是否可以定义has_many :through关系;一个例子显示标准医生,预约,患者的例子就足够了。

为了举例,我们说这些表是:

res_doctor
res_appointment
res_patient

,模型是:

ModelDoctor
ModelAppointment
ModelPatient

我想知道这样的事情是否可能。

如果有可能,如果有人可以修改这个例子以适应我所描述的场景,我会很高兴:

class ResDoctor < ApplicationRecord
  self.table_name = 'res_doctor'

  # Add code here
end

class ResAppointment < ApplicationRecord
  self.table_name = 'res_appointment'

  # Add code here
end

class ResPatient < ApplicationRecord
  self.table_name = 'res_patient'

  # Add code here
end

谢谢!

PS:这不是mentioned question的副本。如果不理解问题,请不要将问题标记为重复。

1 个答案:

答案 0 :(得分:1)

这种方式应该有效,Duplicate Link

class Doctor < ApplicationRecord
  self.table_name = "custom_name"


  has_many :appointments   
  has_many :patients, through: :appointments 
end