Add a reference column migration in Rails

Ruby On Rails     Published at 12 days ago    Arjun Ghimire

Share This

Add a reference column migration in Rails

When you already have authors and books tables and wish to add a new relationship between them

All you need to do is: just generate a migration using the following command:

rails g migration AddAuthorsToBooks author:references

Which will create a migration file as:

class AddAuthorsToBooks < ActiveRecord::Migration
  def change
    add_reference :books, :author, index: true
  end
end

Arjun Ghimire

I am a professional software developer, and founder of Careous IT. I love to code in Ruby,React,PHP,Python and contribute in open-source projects.
get in touch

Tarkeshwor,Kathmandu

9840122530,9811932094