Elixir/Phoenix query incl quantity
<p>I have following tables in my db:</p>

<pre><code>schema "foods" do
field :name, :string
has_many :order_items, MyApp.OrderItem
end

schema "order_items" do
field :quantity, :integer, default: 1
field :price, :integer
belongs_to :food, MyApp.Food
</code></pre>

<p>Order items keeps the food_id, as well as quantity. Now I'm trying to create a query that will show how much each food item has been ordered. This is what i have so far:</p>

<pre><code>fooditems = Repo.all(from p in OrderItem, join: f in Food, on: p.food_id == f.id,
                     group_by: f.name, select: {f.name, count(p.id)}, order_by: count(p.id))
</code></pre>

<p>Basically, I have two questions: 1) How can I make quantity field(from order item table) be also included in the query count? For example, if in order item the quantity is 2, the count() will only count is as only 1, so how can I make the count() to count not only by id, but also include the quantity(from order item table, quantity field)? </p>

<p>Additionally, I'm not sure how to call the query from the html. I've tried</p>

<pre><code>            &lt;%= for item &lt;- @fooditems do %&gt;
                &lt;tr&gt;
                &lt;td&gt;&lt;%= item.name %&gt;&lt;/td&gt;
                &lt;/tr&gt;
            &lt;% end %&gt;
</code></pre>

<p>But it returns argument error, so I believe, I'm just calling it the wrong way with item.name. Generally, I want to have a table that will list food names, and the respective quantity (from order_item table, incl quantity field). </p>

<p>Thanks</p>