Reputation: 58
I was writing a script that could generate a model as -----rails g modelname--- for a JSON File but in case I have model with same name, then there arises a conflict, also while running the migration, the schema.rb files will ignore all the relationships for the conflicting model name
---for an example, phone----belongs_to :users, phone----belongs_department now when I run the migration, only the user_id is placed in the schema.rb file, but I need both the columns... user_id and deparment_id
Upvotes: 1
Views: 164
Reputation: 807
Hi you can easily include both user_id as well as department_id. It is not necessary that you should get it done through console only. In your migration file you can manually write it into your migration file as:
add_foreign_key :<this_table_name>, :<table_name_from_which_foreign_key_you_want_to_add>, column: :<column_name>, name: "<name_by which_you_want_to_refer>_FK", on_delete:cascade
This is sample structure which i use for clear understanding.
In your case it would be like:
add_foreign_key :Phone, :User, column: :user_id, name: "user_id_FK"
add_foreign_key :Phone, :Department, column: :dept_id, name: "dpept_id_FK"
Upvotes: 3