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 lib/rumbl/multimedia/video.ex:

 schema ​"​​videos"​ ​do
  field ​:description​, ​:string
  field ​:title​, ​:string
  field ​:url​, ​:string
  field ​:user_id​, ​:id
 
  timestamps()
 end

Our schema sets up a user_id field, of type :id, while our migration defines a :user_id foreign key. To relate our data at the schema level, we need to tell Ecto about our Video to User association. Replace your field :user_id, :id line in your video schema with the ...

Get Programming Phoenix 1.4 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.