我有一个Statistics控制器,它获取date_from与date_to计算之间进行收入的订单数量和金额date_from和date_to.
def calculate(conn, %{"statistics" => %{"date_from" => date_from, "date_to" => date_to}}) do
revenue = Repo.one(from p in Order, where: p.created_date >= ^date_from and p.created_date <= ^date_to, select: sum(p.paied))
order = Repo.one(from p in Order, where: p.created_date >= ^date_from and p.created_date <= ^date_to, select: count(p.id))
conn
|> put_resp_content_type("application/json")
|> render("statistics.json", order: order, revenue: revenue)
end
Run Code Online (Sandbox Code Playgroud)
以下是statistics.view和router,它会导致错误.
def render("statistics.json", %{order: order, revenue: revenue}) do
%{
order: order
}
%{
revenue: revenue
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试删除一个项目及其所有子项:
where(ParentModel, [x], x.user_id == 123)
|> first
|> Repo.delete()
# or
# Repo.delete(where(ParentModel, [x], x.user_id == 123))
Run Code Online (Sandbox Code Playgroud)
两者都抛出异常:
function Ecto.Query.__changeset__/0 is undefined or private
Run Code Online (Sandbox Code Playgroud)
它与验证有什么关系?
在父模型中,我有 on_delete: :delete_all
试图让我的第一个Phoenix应用程序连接到Compose的Cloud Hosted Postgres DB并且无法连接.我已经确认它已启动并正在运行.我收到以下错误:
[error] GenServer #PID<0.178.0> terminating
** (DBConnection.ConnectionError) tcp connect (postgres://*****@aws-us-east-1-portal.5.dblayer.com:16786/compose:5432): non-existing domain - :nx
domain
(db_connection) lib/db_connection/connection.ex:148: DBConnection.Connection.connect/2
(connection) lib/connection.ex:622: Connection.enter_connect/5
(stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
** (Mix) The database for HelloPhoenix.Repo couldn't be created: an exception was raised:
** (DBConnection.ConnectionError) tcp connect (postgres://****@aws-us-east-1-portal.5.dblayer.com:16786/compose:5432): non-existing domain -
:nxdomain
(db_connection) lib/db_connection/connection.ex:148: DBConnection.Connection.connect/2
(connection) lib/connection.ex:622: Connection.enter_connect/5
(stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Run Code Online (Sandbox Code Playgroud)
这些是我的依赖:
defp deps do
[{:phoenix, "~> 1.2.1"},
{:phoenix_pubsub, "~> 1.0"},
{:phoenix_ecto, "~> 3.0"},
{:postgrex, ">= 0.0.0"},
{:phoenix_html, …Run Code Online (Sandbox Code Playgroud) 我正在创建一个简单的服务,它接收一个电子邮件地址 - 并从用户列表中找到用户.
这是一个包含用户列表的简化版本.我想根据用户的电子邮件地址提取用户.
def endpoint do
[%{email: "foo@example.org", account_type: "full"},
%{email: "bar@earxample.org", account_type: "standard"},
%{email: "baz@example.org", account_type: "full"}]
end
def get_by_email(user, email) do
user |> Map.get(:email)
end
def dev_endpoint(email) do
endpoint
|> Enum.map(&get_by_email(email)/1)
end
def show(conn, %{"id" => email}) do
response = dev_endpoint(email)
json(conn, %{"email" => response})
end
Run Code Online (Sandbox Code Playgroud)
基本上:
dev_endpoint("foo@example.org")
Run Code Online (Sandbox Code Playgroud)
应该返回:
%{email: "foo@example.org", account_type: "full"}
Run Code Online (Sandbox Code Playgroud)
我知道我的捕获语法有问题,但我尝试了各种不同的迭代,没有运气.
在我的Elixir/Phoenix应用程序中,我想在html页面上的mix.exs文件中显示来自git和版本号的当前提交哈希.我怎样才能做到这一点?也就是说,是否可以访问这种信息?
这个问题也被标记为"Erlang",因为这些Elixir模块或多或少只是包装了Erlang功能.
节点看起来像命名过程.它们可以同时执行功能,链接到其他节点,并像流程监控器一样工作.每个模块中的许多功能看起来都是一样的,增强了相似之处.
Node模块的价值是什么?Process没有提供什么?
我的Elixir/Phoenix应用程序需要能够录制HTML5视频,允许用户查看他们录制的视频,然后通过AJAX将录制内容直接上传到S3进行存储,并向Elixir服务器发送POST以存储新上传的对象.在Rails世界中,有关于如何实现这一目标的各种宝石和冗长的教程; 在Elixir/Phoenix进行直接到S3文件上传的简单方法是什么?
我正在使用Elixir OptionParser,但我在解析长的虚假参数时遇到了问题.
最好,我想做这样的事情:
OptionParser.parse(argv, strict: ["db-username": :string, "db-password": :string])
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用.我到了Unknown option.
有人知道是否有可能以某种方式?
https://github.com/wojtekmach/acme_bank/blob/master/apps/bank/lib/bank/model.ex#L10
alias Bank.{
Account,
Customer,
Deposit,
Ledger,
Repo,
Transfer,
Entry
}
Run Code Online (Sandbox Code Playgroud)
没有它,我得到一个Ecto.Queryable not implemented for Foo, the given module does not exist。如何将Ecto查询内容引入模型中?
我在Elixir中编写了一个函数,它创建了一个映射,然后尝试更改其中一个键的值,但它似乎不想更新.
defmodule KV do
def test() do
map = %{foo: "bar", hello: "world"}
IO.inspect map.foo
%{map | foo: "baz"}
IO.inspect map.foo
end
end
KV.test()
Run Code Online (Sandbox Code Playgroud)
编译代码后我期望出现的是:
"bar"
"baz"
Run Code Online (Sandbox Code Playgroud)
但我真正得到的是:
"bar"
"bar"
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试在iex shell上键入它时,它会执行它应该执行的操作:
iex(1)> map = %{foo: "bar", hello: "world"}
%{foo: "bar", hello: "world"}
iex(2)> %{map | foo: "baz"}
%{foo: "baz", hello: "world"}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了许多其他方法来更新值,但似乎没有任何工作.