Set belongs to while adding foreignkey to the model in rails

Asked 2 years ago, Updated 2 years ago, 45 views

The model did not contain the original author information (user). You are about to add a user.

To migrations

add_reference : model, :user, index: true, foreign_key: true

So I'd like to add author information. Then should I write belongs_to separately on the original model?

Also, I want to give the default value only during migration, but can I give the default value as user_id like default: 10?

ruby-on-rails migration

2022-09-22 21:32

1 Answers

It's a self-answer.

You can write bellongs_to separately. Migration is a task to add columns to DB, and belongs_to is necessary for use with model.user, so you can proceed with the migration by adding both migration and belongs_to to the code.

The default value is not included. After the migration, I put the value in the DB separately.


2022-09-22 21:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.