Skip to content

Instantly share code, notes, and snippets.

@rhiroyuki
Last active April 3, 2021 18:45
Show Gist options
  • Save rhiroyuki/5abbe94ec1b72b4acfbc54ed166cfad2 to your computer and use it in GitHub Desktop.
Save rhiroyuki/5abbe94ec1b72b4acfbc54ed166cfad2 to your computer and use it in GitHub Desktop.

Ecto

Getting the first record

In ActiveRecord

User.first

In Ecto

Repo.one(from x in Model, order_by: [asc: x.id], limit: 1)

# You can hide the order_by since it is ascending by default
Repo.one(from x in Model, limit: 1)

Getting the last record

ActiveRecord

User.last

Ecto

Repo.one(from x in Model, order_by: [desc: x.id], limit: 1)

Querying

Repo.all(from x in SomeModel, where: x.greeted == true)

Prying

require IEx; IEx.pry

Run the server with the following command:

iex -S mix phx.server

Reloading

# recompile

Scaffold

$ mix phx.gen.context Todos Todo todos title:string done:boolean

* creating lib/live_view_todos/todos/todo.ex
* creating priv/repo/migrations/20210403184352_create_todos.exs
* creating lib/live_view_todos/todos.ex
* injecting lib/live_view_todos/todos.ex
* creating test/live_view_todos/todos_test.exs
* injecting test/live_view_todos/todos_test.exs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment