O'Reilly logo

Programming Phoenix by Jose Valim, Bruce Tate, Chris McCord

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required