我有一个简单的before_save转换,并了解到phoenix使用Ecto changest来完成这项任务.
我的Stage模型有一个position属性,默认为current maximum + 1如此尝试实现如下:
舞台模型:
def changeset(struct, params \\ %{}) do
struct
|> cast(params, @required_fields, @optional_fields)
|> validate_required([:name])
|> set_position
end
defp set_position(current_changeset) do
# get current max position from db
max_position = Repo.one(
from s in Stage,
select: fragment("COALESCE(MAX(?),0)", s.position)
)
case current_changeset do
%Ecto.Changeset{valid?: true} ->
put_change(current_changeset, :position, max_position+1)
_ ->
current_changeset
end
end
Run Code Online (Sandbox Code Playgroud)
在逐个插入记录时工作正常但在批量插入时失败; 例如在下面的seed文件中.
种子
alias MyApp.{Repo, Post}
[
%{name: "Requirements"},
%{name: "Quotation"},
%{name: "Development"},
%{name: …Run Code Online (Sandbox Code Playgroud) 我想在Elixir中编码这个算法:
var name = generate_name();
while (check_if_exists(name)) {
name = generate_name();
}
Run Code Online (Sandbox Code Playgroud)
我无法在Elixir中以相同的方式编码,因为必须有更多的惯用和功能方式.那怎么办呢?
我有Phoenix应用程序(我使用--api标志创建了它),其中包含一些处理我存储在中的请求所需的其他数据priv/data/filters.csv。
因此,它在dev模式下可以正常工作,我可以像这样读取该文件:File.stream!("priv/data/filters.csv"),但是使用mix edeliver update该文件交付应用后,将无法再访问它。
我可以在构建目录中看到该文件夹,但是在交付应用程序之后,没有该文件夹priv。我不确定为什么不将其复制到传递文件夹中。
我做错了什么?交付过程后是否需要复制这些文件?在哪里可以存储必须以生产方式使用的文件?
Elixir Decimal包中是否有等效的is_string,is_number,is_integer,is_binary?
如果没有,为Decimal进行模式匹配的方法是什么?
在Erlang中,可以编写'1'一个以整数命名的原子.Elixir使用语法:<name>来定义原子,但:1不可能:
iex(1)> :1
** (SyntaxError) iex:1: unexpected token: ":" (column 1, codepoint U+003A)
Run Code Online (Sandbox Code Playgroud)
Elixir有没有办法生成一个整数命名的原子?
我有以下型号:
schema "users" do
field :email, :string
field :subscriptions, {:array, :string}
timestamps()
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:email,:subscriptions])
end
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何从我的控制器更新subscriptions-array.我希望简单地将一个新字符串推送到数组:
def subscribe(conn, %{"category" => category} ) do
IO.puts category # Will print "whatever_category"
user = Repo.get(User, conn.assigns.user.id)
changeset = User.changeset(user, ?)
Repo.update(changeset)
Run Code Online (Sandbox Code Playgroud)
我尝试过以下方法:
changeset = User.changeset(user, %{subscriptions: ["test"]})
Run Code Online (Sandbox Code Playgroud)
这会为数组添加"test",但如果我添加另一个值,它将被替换.
谢谢!
在我的Phoenix应用程序中,我想向URL添加查询字符串:
some_cool_path(@conn, :index, "view-mode": "table")
Run Code Online (Sandbox Code Playgroud)
我希望它会生成类似的URL /some_cool?view-mode=table,但会引发异常:
protocol Phoenix.Param not implemented for ["view-mode": "table"]
Run Code Online (Sandbox Code Playgroud)
我该如何解决?
我有这个:
a1 = [%{id: 1, val: 12}, %{id: 3, val: 7}, %{id: 1, val: 5}, %{id: 2, val: 3}, %{id: 2, val: 5}], %{id: 1, val: 3}]
Run Code Online (Sandbox Code Playgroud)
我怎么能得到这个?
%{
1 => 20,
2 => 8,
3 => 7
}
Run Code Online (Sandbox Code Playgroud)
也就是说,按"id"分组的每个项目的"val"之和
我应该先用"id"将它们分组吗?
Enum.group_by a1, &(&1.id)
# =>
%{
1 => [%{id: 1, val: 12}, %{id: 1, val: 3}, %{id: 1, val: 5}],
2 => [%{id: 2, val: 3}, %{id: 2, val: 5}],
3 => [%{id: 3, val: 7}]
}
Run Code Online (Sandbox Code Playgroud)
然后做map …
首先,我在Windows 10中开发.我vcvarsall.bat amd64在每次编译之前运行.我在用:
Elixir 1.4.2
凤凰城v1.2.1
我开始了一个全新的项目,制作了一个用户表,一切都运行良好.我添加了comeonin哈希pw,我不能再创建用户.我得到一个错误页面说:
function Comeonin.Bcrypt.hashpwsalt/1 is undefined (module Comeonin.Bcrypt is not available)
以下是相关文件的代码:
Mix.exs
# mix.exs
...
def application do
[mod: {PollarAppV2, []},
applications: [:phoenix, :phoenix_pubsub, :phoenix_html, :cowboy, :logger, :gettext,
:phoenix_ecto, :postgrex, :comeonin, :timex]]
end
...
defp deps do
[{:phoenix, "~> 1.2.1"},
{:phoenix_pubsub, "~> 1.0"},
{:phoenix_ecto, "~> 3.0"},
{:postgrex, ">= 0.0.0"},
{:phoenix_html, "~> 2.6"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:gettext, "~> 0.11"},
{:cowboy, "~> 1.0"},
{:comeonin, "~> 3.0"},
{:timex, "~> 3.0"}]
end
...
Run Code Online (Sandbox Code Playgroud)
User.ex …
请帮助我解决有关Elixir与Ruby性能的基准问题.
我尝试在两种语言中实现相同的阶乘,而Ruby显示出比Elixir更好的结果:
# ruby_factorial_with_iterator.rb
def factorial_with_iterator(n)
res = 1
(1..n).each{|time| res *= time}
res
end
p "factorial_with_iterator(200000)"
p factorial_with_iterator(200000)
Run Code Online (Sandbox Code Playgroud)
运行后:
$ time ruby ruby_factorial_with_iterator.rb
real 0m18.378s
user 0m17.348s
sys 0m0.844s
Run Code Online (Sandbox Code Playgroud)
和两个Elixir例子:
# elixir_factorial_with_iterator.exs
defmodule FactorialWithIterator do
def of(n) do
Enum.reduce(1..n, 1, &*/2)
end
end
IO.puts "Factorial of 200000: "
IO.puts FactorialWithIterator.of(200000)
Run Code Online (Sandbox Code Playgroud)
运行后:
$ time elixir elixir_factorial_with_iterator.exs
real 1m1.735s
user 1m1.556s
sys 0m0.104s
Run Code Online (Sandbox Code Playgroud)
另一个例子:
# elixir_factorial_with_recursion.exs
defmodule FactorialWithRecursion do
def of(0), do: 1
def of(n) when n > 0 do
n …Run Code Online (Sandbox Code Playgroud) elixir ×10
bcrypt ×1
benchmarking ×1
compare ×1
distillery ×1
ecto ×1
edeliver ×1
loops ×1
performance ×1
ruby ×1
while-loop ×1