I'm a beginner and I can't move a table column(doctors.specialty) to another table column(specialties.name).
This is the doctors table:
tp Doctor.all
| ID | FIRST_NAME | LAST_NAME | SPECIALTY | ZIP_CODE | CREATED_AT | UPDATED_AT | CITY_ID |
|---|---|---|---|---|---|---|---|
| 1 | Cathy | Wyman | Gynecologist | 78328 | 2022-07-28 13:48:57 | 2022-07-28 14:33:51 | 17 |
| 2 | Dave | Howell | General practitioner | 97544 | 2022-07-28 13:48:57 | 2022-07-28 14:33:51 | 13 |
| 3 | Zachariah | Stamm | Anesthesiology | 08435-1702 | 2022-07-28 13:48:57 | 2022-07-28 14:33:51 | 4 |
| 4 | Forrest | Koelpin | General practitioner | 58486 | 2022-07-28 13:48:57 | 2022-07-28 14:33:51 | 16 |
| 5 | Cameron | Zieme | Anesthesiology | 01200 | 2022-07-28 13:48:57 | 2022-07-28 14:33:51 | 13 |
| 6 | Chia | Borer | Gynecologist | 81913-8875 | 2022-07-28 13:48:57 | 2022-07-28 14:25:11 | 18 |
| 7 | Sonny | Torphy | Gynecologist | 08593-7841 | 2022-07-28 13:48:57 | 2022-07-28 14:33:51 | 4 |
I generated a new model:rails g model Specialty name:string doctor_id:integer
I generated a new migration
rails g migration MoveColumnDataToSpecialty
I tried this code but it didn't work and i'm not sure that I should use it to move the column
class MoveColumnDataToSpecialty < ActiveRecord::Migration[5.2]
def self.up
add_column :users, :some_property, :string
execute "UPDATE users u SET some_property = p.some_property FROM profiles p WHERE u.id = p.user_id;"
remove_column :profiles, :some_property
end
def self.down
add_column :profiles, :some_property, :string
execute "UPDATE profiles p SET some_property = u.some_property FROM users u WHERE p.user_id = u.id;"
remove_column :users, :some_property
end
end
Thank you in advance
rails g model Specialty name:string doctor_id:integer-- don't sure it's good idea. Columndoctors.speciality_idlooks better thanspecialities.doctor_id- mechnicov