has_many, belongs_to relation in active record migration rails 4

前端 未结 6 1679
粉色の甜心
粉色の甜心 2020-12-12 15:28

I have a User model and a Task model. I have not mentioned any relation between them while creating them.

I need to establish that U

6条回答
  •  心在旅途
    2020-12-12 16:16

    There is no special migration command that would be used.

    In your User model you will put

    class User < ActiveRecord::Base
      has_many :tasks
    end
    
    class Task < ActiveRecord::Base
      belongs_to :user
    end
    

    In the corresponding migration file for the tasks you have the following field added user_id

    Take a look at this guide

提交回复
热议问题