You could call:
rails g model task user:references
which will generates an user_id
column in the tasks
table and will modify the task.rb
model to add a belongs_to :user
relatonship. Please note, you must to put manually the has_many :tasks
or has_one :task
relationship to the user.rb
model.
If you already have the model generated, you could create a migration with the following:
rails g migration AddUserToTask user:belongs_to
which will generate:
class AddUserToTask < ActiveRecord::Migration
def change
add_reference :tasks, :user, index: true
end
end
the only difference with this approach is, the belongs_to :user
relationship in the task.rb
model won't be created automatically, so you must create it for your own.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…