Do you miss `User.create(params)`? Try BaseModel, ActiveRecord for Ecto.
Db.Repo.all(from c in Comment, where: c.post_id == ^post_id)
Ecto has always seemed more verbose, and harder to read than it needs to be. I’d rather write:
Comment.where(post: post)
Don’t want to write CRUD for every model? Let BaseModel write it for you: add use BaseModel, repo: MyApp.Repo
to the top of your models, and immediately gain a fluent API for your data:
-
create(params)
-
all
-
find(id)
-
first(where_clause)
-
first_or_create(where_clause)
-
where(where_clause)
-
count(where_clause \\ [])
-
update(struct, params)
-
update_where(where_clause, params)
-
delete(id_or_struct)
-
delete_where(where_clause)
-
delete_all
BaseModel resolves associations automatically, supports Ecto validations, saves lots of keystrokes on the console while developing and debugging, and encourages good separation of persistence code from the rest of your app.
Check it out on Github.
Read next How to automatically run stale tests on file change in Elixir