Should I use Ecto.Repo in Controller or Model for Elixir Phoenix?

帅比萌擦擦* 提交于 2019-11-30 19:01:11

You should keep your Repo calls inside your controller. If your logic is complicated then you should consider moving the logic out into its own service module.

You should treat your model functions as pure (free from side effects) so they should only act on data. So for example you could have:

def alphabetical(query)
  order_by(query, [u], u.name)
end

But you should not have:

def alphabetical(query)
  order_by(query, [u], u.name)
  |> Repo.all
end

This is because queries are purely data, the call to Repo.all has side effects (going off to the database) so it belongs in your controller.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!