Ich habe ein Modell Patient
, dass has_many
Phenotype
s durch PatientsPhenotype
s.Einstellung has_many durch Sammlungen durch Ändern der Fremdschlüsselwerte in Join-Tabelle
class Patient < ActiveRecord::Base
has_many :patients_phenotypes
has_many :phenotypes, through: :patients_phenotypes
end
class PatientsPhenotype < ActiveRecord::Base
belongs_to :patient
belongs_to :phenotype
belongs_to :user
has_many :properties # Information associated with each row in this join table
end
class Phenotype < ActiveRecord::Base
has_many :patients_phenotypes
has_many :patients, through: :patients_phenotypes
end
Da einige Patientenakten nicht mehr aktuell sind, möchte ich gehört alte Patientenakten zu neuen Patientenakten, die Phänotypen neu zu verknüpfen. Ich kann die Phänotypen nicht einfach direkt zu den Patienten hinzufügen, die patient.phenotypes << phenotype
verwenden, da die Verknüpfungen andere Information enthalten, die an den Join-Tabellendatensatz selbst wie user_id
und properties
gebunden ist.
Was ich versucht habe, um dies zu erreichen, ist, den FK einer Join-Tabelleninstanz vom alten Patienten auf den neuen Patienten zu setzen und dann diese Join-Tabelleninstanz der Join-Tabellensammlung des neuen Patienten hinzuzufügen.
patient_phenotype.patient_id
# => (old_patient_id)
patient_phenotype.patient_id = new_patient.id
patient_phenotype.save
new_patient.patients_phenotypes << patient_phenotype
Dies ist jedoch nicht funktioniert:
new_patient.phenotypes
# => []
Was falsch ist hier los? Wäre es besser, wenn ich einfach ein ganz neues PatientsPhenotype
erstelle und ihm die Informationen des ursprünglichen PatientsPhenotype
wieder zuordne?
Schön! Ich wünschte, ich wüsste, dass das notwendig ist. Ich merke jedoch, dass ich nachladen musste, nachdem ich irgendwelche Änderungen am Modell gespeichert hatte. – Vardarac
Alternativ können Sie 'inverse_of' in Ihren Modellen verwenden. – Uzbekjon