Adding Categories

In this section, we’re going to add some categories. We’ll use many of the same techniques we discovered in our user-to-video relationship to manage the relationships between videos and categories. A video optionally belongs to a category, one chosen by the end user. First, let’s generate the model and migration, using phoenix.gen.model, like this:

 $ ​​mix​​ ​​phoenix.gen.model​​ ​​Category​​ ​​categories​​ ​​name:string
 
 * creating priv/repo/migrations/20150829145417_create_category.exs
 * creating web/models/category.ex
 * creating test/models/category_test.exs

This generator will build the model with a schema and migration for us.

Generating Category Migrations

Next let’s edit our migration to mark the name field as ...

Get Programming Phoenix now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.