Skip to content

Instantly share code, notes, and snippets.

@talum
Last active August 17, 2017 03:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talum/4fbb4a1f21fed16344f5f76275a04271 to your computer and use it in GitHub Desktop.
Save talum/4fbb4a1f21fed16344f5f76275a04271 to your computer and use it in GitHub Desktop.
programming phoenix ch 6 generators and relationships

Programming Phoenix Chapter 6: Generators and Relationships

Generating Resources

  • phoenix.gen.html
  • phoenix.gen.json

"At the end of the day, it ends up adding complexity to the framework—and, indirectly, to your application—only to save a few keystrokes every once in a while. " (p.93)

ex) mix phoenix.gen.html Video videos user_id:references:users url:string title:string description:text

Authentication Refactor

  • move defp authenticate to the Rumbl.Auth module
  • make it public
  • share it across controllers by adding it to the web controller import
  • add it to pipe_through in the router, within the /manage scope

Generated Controller & View

  • scrub_params plug to protect against submission of empty strings

Generated Migrations

  • similar to Rails generator

Building Relationships

  • schema: "This file is responsible for identifying the fields in a way that ties in to both the database table and the Elixir struct." (p.99)
  • @variable means module attribute
  • Ecto Associations are explicit; must ask expicitly for related data
  • rewrite the action function in the controller to pass in the current user explicitly
  • "Using Ecto.assoc, we built a simple authorization rule restricting deletes and updates to the video’s owner." (p.104)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment