In a Phoenix Framework application I have a model Product
with the field name
. I want to create a mix task which lists all product names.
In Ruby on Rails this would be the code to solve the problem:
namespace :list do
desc "List all products"
task products: :environment do
Product.all.each do |product|
puts product.name
end
end
end
In Phoenix I can't even get the list of all products from the database. Here is the task code:
lib/mix/tasks/list.product.ex
defmodule Mix.Tasks.List.Product do
use Mix.Task
import Mix.Ecto
alias App.Repo
alias App.Product
def run(_args) do
products = Repo.all(Product)
end
end
When I run this task I get this error message:
** (UndefinedFunctionError) function Ecto.Queryable.__using__/1
is undefined or private
What do I have to do to fetch all products from the database in this mix task?