Phoenix Web Development
上QQ阅读APP看书,第一时间看更新

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).