Associations I

Models II

What did we just do?

  1. We created two models named Tag and Destinations.

  2. In the model files, we used the methods has_many and belongs_to define an association between Tag and Destination:

    • has_many :destinations denotes that a single Tag can have multiple Destinations.
    • belongs_to :tag denotes that each Destination belongs to a single Tag.

The has_many / belongs_to pair is frequently used to define one-to-many relationships. A few examples are:

  • a Library has many Books; a Book belongs to a Library
  • an Album has many Photos; a Photo belongs to an Album
  • a Store has many Products; a Product belongs to a Store
Community Forums
Get help and ask questions in the Codecademy Forums
Report a Bug
If you see a bug or any other issue with this page, please report it here.