What did we just do?
We created two models named Tag and Destinations.
In the model files, we used the methods
belongs_to define an association between Tag and Destination:
has_many :destinationsdenotes that a single Tag can have multiple Destinations.
belongs_to :tagdenotes that each Destination belongs to a single Tag.
belongs_to pair is frequently used to define one-to-many relationships. A few examples are:
Now that there’s an association between Tag and Destination, let’s continue and add columns to the migration files.
Open the migration file in db/migrate/ for the tags table, and add the following columns:
Next in the migration file for the destinations table, add the following columns:
Run the migration to update the database with Tag and Destination.
Open up db/seeds.rb. We’ve added a few items here to seed the database with tags and destinations. Run
bundle exec rake db:seed to seed the database with the data in db/seeds.rb.