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

Building Relationships

After the migration, Ecto generated a schema. This file is responsible for identifying the fields in a way that ties in to both the database table and the Elixir struct. Now let’s take a look at the schema in web/models/video.ex:

 schema ​"​​videos"​ ​do
  field ​:url​, ​:string
  field ​:title​, ​:string
  field ​:description​, ​:string
  belongs_to ​:user​, Rumbl.User
 
  timestamps
 end

Our schema sets up a belongs_to association, defining a :user_id field of type :integer and an association field. Our migration defines a :user_id foreign key. Ecto will use these elements to build the right association between our models.

The video module also includes a changeset ...

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