官术网_书友最值得收藏!

  • Phoenix Web Development
  • Brandon Richey
  • 193字
  • 2021-08-27 18:28:29

Creating a Votes context

Our module definition will be Vocial.Votes (not Vocial.Votes.Votes), and in it we'll want to import Ecto.Query so we can build out any special queries that may be required. We'll also want to alias in our Repo, Poll, and Option modules, just like we did in our IEx window. So, our starting skeleton should look like this:

defmodule Vocial.Votes do
import Ecto.Query, warn: false

alias Vocial.Repo
alias Vocial.Votes.Poll
alias Vocial.Votes.Option
end
The warn: false option on the import and alias statements tells Elixir not to give warnings when we import or alias something and then don’t use it. It's a good thing to use important functionality imports such as Ecto.Query in a Context, but you should generally try to avoid using warn: false whenever you can, as it can hurt the readability and maintainability of your code long-term!

Okay, so now that we have a nice, handy, single interface to grab data out of the database, we should start by writing something that does precisely that. Let's grab a list of all of the Polls out of the database (and make sure we include our Options with it).

主站蜘蛛池模板: 同德县| 宁远县| 武川县| 全州县| 朝阳市| 陇川县| 兴宁市| 哈尔滨市| 东辽县| 望都县| 平南县| 昔阳县| 罗田县| 娱乐| 临潭县| 龙南县| 壤塘县| 建昌县| 永顺县| 南投市| 卫辉市| 华阴市| 化德县| 义乌市| 申扎县| 尼勒克县| 泰来县| 北京市| 阿拉善左旗| 新邵县| 黎平县| 陇川县| 名山县| 博白县| 新沂市| 闽清县| 焦作市| 徐汇区| 新源县| 绵阳市| 竹山县|