- database constraints, adding associations with references p. 109 - makes sure the category_id referenced in videos exists
- i.e. make the database maintain integrity
- uniqueness constraint on categories, to prevent dupes. esp. when running seed file twice
- are composable, can define in chunks
This strategy works because Ecto defines something called the queryable protocol. from receives a queryable, and you can use any queryable as a base for a new query.
(p. 112)
Category
|> Category.alphabetical
|> Category.names_and_ids
categories = Repo.all query
assign(conn, :categories, categories) end
"Repo.one doesn’t mean “return the first result.” It means “one result is expected, so if there’s more, fail.” (p. 115)
- query normalization at compile time, means it's easier to find type errors (querying a text field for a string)
- using the ^ operator to pattern match without assignment
- keep functions with side effects in the controller
- as long as it's queryable, Repo can accept it
"A query fragment sends part of a query directly to the database but allows you to construct the query string in a safe way."
- escape hatch for writing custom queries