0
votes

Many Rails tutorials depict the setup of an ActiveRecord association as requiring a database migration that establishes a foreign key (like with a has_many/belongs_to relationship, for instance). Yet other documentation seems to leave out the migration part altogether, just updating the models, nesting the routes, etc.

Is a migration always necessary to tie the two resources together, or is it just necessary for certain types of associations?

2

2 Answers

1
votes

You must always have foreign keys between the tables that are related, for example: if you have two models User and Task, you need to store which user a task belongs to, so you always need foreign keys for relations, you could create them manually in your database without needing a migration but this is a very bad practice because migrations provide rollbacks and are essential when you work with others on the same project.

1
votes

Yes its nessesary for creating associations. But somewhere i saw the gem which create asscosiations just for annotate has_many/belongs_to attributes in Model.