我有一个患者模型和一个客户模型。患者可以拥有许多客户,而客户可以拥有许多患者。我想为关联创建一个模型。我怎么称呼它?
客户是医院或医生的办公室。患者是需要医院或医生办公室教育的人。
答案 0 :(得分:2)
什么是客户?它是某种医生吗?
registrations
或enrollments
怎么样?
然后你有:
# client
has_many :enrollments
has_many :patients, :through => :enrollments
# patient
has_many :enrollments
has_many :clients, :through => :enrollments
答案 1 :(得分:0)
如果您想遵循约定,那么连接表中的名称只需按字母顺序排序:
# create_clients_patients.rb
create_table "clients_patients", :id => false do |t|
t.column "client_id", :integer, :null => false
t.column "patient_id", :integer, :null => false
end
答案 2 :(得分:0)
如果您需要访问模型类,则可能不必使用多对多 协会但是:
has_many :through
您可以根据自己的喜好命名的关联。
多对多关联只有表命名约定(在您的示例中为clients_patients),因为您不需要直接访问交叉表模型。