我正在尝试插入发票结构及其关联的发票项目.我可以插入发票数据,并调用匿名函数来验证,转换和插入每个项目.由于insert/2不会产生返回,如果一个项目未通过验证或插入,如何在仍然能够回滚整个事务的同时获取项目的invoice_id?
我把代码放在我自己的回购中,这里是:
def insertassoc(params) do
Repo.transaction(fn ->
i = Invoice.changeset(params["data"], :create)
if i.valid? do
Repo.insert(i)
else
Repo.rollback(i.errors)
end
insert_include = fn k ->
c = InvoiceItem.changeset(k, :create)
if c.valid? do
Repo.insert(c)
else
Repo.rollback(c.errors)
end
end
for include <- params["includes"] do
insert_include.(Map.merge(include, %{"invoice_id" => ????}))
end
end)
end
Run Code Online (Sandbox Code Playgroud)
以下是我在控制器中使用它的方法:
def create(conn, params) do
case InvoiceRepo.insertassoc(params) do
{:ok, x} ->
json conn, Map.merge(params, %{"message" => "OK"})
{:error, x} ->
json conn |> put_status(400), Map.merge(params, %{"message"
=> "Error"})
end
end
Run Code Online (Sandbox Code Playgroud)
有没有很多最新的例子与Ecto,所以抱歉,如果这些是noob问题;-).有人有想法吗?我尝试将发票插入放在私有函数中,并使用case块来确定主事务是否应回滚,但我无法弄清楚如何从中获取发票ID.
有没有办法预先加载ecto关联而不显式使用preload:?
类似于架构中的选项?
schema "gadgets" do
field :foo,
has_many :bars, Myapp.Bar, preload: true
end
Run Code Online (Sandbox Code Playgroud)
我正在做类似的事情
Repo.get(Gadget, id)
|> Repo.preload: [:bars]
Run Code Online (Sandbox Code Playgroud)
编辑:我试图这样做的原因是因为我想将相关模型预加载到已经预加载的相关模型,如
preload: [:invoices preload: :items]
Run Code Online (Sandbox Code Playgroud) 像elixir中的其他函数(以及Ecto自己的事务),我想模式匹配以处理Ecto查询的潜在错误.像这样:
case Repo.get!(User, id) do
{:ok, user} ->
#do something
{:error, message} ->
#pass the error
end
Run Code Online (Sandbox Code Playgroud)
显然这不起作用,但我如何模式匹配Ecto错误,如Ecto.NotSingleResult和其他潜在的查询问题,如预加载错误?
我试图得到像这样的ecto查询:
def find(searchterm) do
query = from c in Contact,
#where: fragment("? % ?", c.company_name, ^searchterm),
where: like(c.company_name, ^searchterm),
contacts = Repo.all(query)
{:ok, contacts}
end
Run Code Online (Sandbox Code Playgroud)
在我的表中,我有一个company_name"Asymptote".使用where:like/2我的查询如下所示:
SELECT c0."id", c0."company_id", c0."company_name" FROM "contacts" AS c0 WHERE (c0."company_name" LIKE $1) ["Asym"] (1.0ms)
Run Code Online (Sandbox Code Playgroud)
当pg_trm搜索取消注释时,它看起来像这样:
SELECT c0."id", c0."company_id", c0."company_name" FROM "contacts" AS c0 WHERE (c0."company_name" % $1) ["Asym"] (1.0ms)
Run Code Online (Sandbox Code Playgroud)
据我所知,查询看起来不错,但没有结果.由于我在向数据库添加"渐近线"之后添加了索引,我希望这就是为什么在pg_trm索引中找不到它,但为什么不喜欢/ 2或ilike/2工作?输入全名"Asymptote"时,我能够找到记录.
我有几个任务,我正在异步运行.根据输入,一个或多个可能会运行很长时间,但只有一个任务将返回:success消息.
slowtask = Task.async(slow())
fasttask = Task.async(fast())
Run Code Online (Sandbox Code Playgroud)
如何捕获上述两个任务中的第一个完成,而不必等待另一个?我已经尝试过了Task.find/2
,但是由于它是用enum实现的,所以它似乎在找到ref/message之前等待所有的退出信号.我的另一个想法是对此进行调查Stream.cycle
,忽略仍然存在的任务并捕获已退出的任务.看来这种灵丹妙药不喜欢以这种方式进行民意调查.
我在凤凰城使用默认的json工具,但由于某种原因我无法返回任何日期(字段类型:日期).我得到这样的东西:
unable to encode value: {2015, 3, 24}
Run Code Online (Sandbox Code Playgroud)
我在类型为date的db中使用了postgres db.我错过了什么吗?在用毒素编码之前,我是否需要构建一个解析日期的函数?
我试图最终得到一个包含许多不同首选项的地图,它应该如下所示:
%{some_preference_name:%{foo:"bar"},another_preference_name:%{foo:"bar"}}
Run Code Online (Sandbox Code Playgroud)
我有一个数据库中的首选项映射列表,我需要通过它们并将"首选项"字段设置为键,其中各种值作为值映射.
我尝试用Enum.reduce和Enum做地图,但我无法将列表正确.
Enum.map(preferences, fn(data)->
Map.put(%{}, data.preference,
%{
foo: data.foo
}
)
end)
Run Code Online (Sandbox Code Playgroud)
收益:
[{some_preference_name:%{foo:"bar"}},{another_preference_name:%{foo:"bar"}}]
Run Code Online (Sandbox Code Playgroud)
然后:
Enum.reduce(preferences, fn(acc, data)->
Map.put(acc, data.preference,
%{
foo: data.foo
}
)
end)
Run Code Online (Sandbox Code Playgroud)
收益:
%{some_preference_name:%{foo:"bar"},preference: "another_preference_name",foo:"bar"}
Run Code Online (Sandbox Code Playgroud)
它得到第一个正确,但不是其余的.据我所知,从Erlang R17开始,我能够添加变量键名的唯一方法是使用Map.put/3.
我使用html文件作为模板,使用以下代码,在dev模式下工作(不在exrm版本中):
{:ok, template} = File.read "priv/static/templates/receipt_template.html"
Run Code Online (Sandbox Code Playgroud)
当发行版与exrm打包时,此相对路径不再有效,并且找不到该文件.是否有更好的方法来声明路径,以便在将其转换为版本时不会搞砸?
我正在使用 Erlang 的 httpc 发出带有参数和基本身份验证标头的 get 请求。当在 httpc/1 中没有标头的情况下使用时,如下所示:
url = String.to_char_list("https://endpoint.com/endpoint?param=foo")
:httpc.request(url)
Run Code Online (Sandbox Code Playgroud)
我收到预期的 403 未经授权。但是,当我像这样使用 httpc/4 时:
url = String.to_char_list("https://endpoint.com/endpoint?param=foo")
headers = headers ++ [authheader]
httpc.request(:get, {url, headers}, [], [])
Run Code Online (Sandbox Code Playgroud)
我收到 404 未找到错误。当从浏览器手动添加 auth 标头时,我可以 IO.puts url 并直接成功访问资源。我的帖子路由在 httpc/4 上一切正常。这里发生了什么事?