我怎样才能从我现在在 Elixir 中得到它的方式得到下面的字符串。
['x1', 'x2']
Run Code Online (Sandbox Code Playgroud)
我尝试过enum.join但没有得到所需的数据
到
'x1,x2'
Run Code Online (Sandbox Code Playgroud) 我有下面的 Ecto 模型。当我尝试渲染时出现错误。如何修改 @derive 以使其预加载?还是我必须写出实现?处理这个问题的推荐方法是什么?
** (RuntimeError) cannot encode association :tilemap_layers from MyProject.Tilemap to JSON because the association was not loaded. Please make sure you have preloaded the association or remove it from the data to be encoded
Run Code Online (Sandbox Code Playgroud)
模型在这里:
defmodule MyProject.Tilemap do
use MyProject.Web, :model
@derive {Poison.Encoder, only: [
:name,
:tile_width,
:tile_height,
:width,
:height,
:orientation,
:tilemap_layers,
:tilesets
]}
schema "tilemaps" do
field :name, :string
field :tile_width, :integer
field :tile_height, :integer
field :width, :integer
field :height, :integer
field :orientation, :string
has_many …Run Code Online (Sandbox Code Playgroud) 我知道,默认情况下,ExUnit.Case 是同步的(根据ExUnit.Case 文档)。我还阅读了这个问题(https://github.com/elixir-lang/elixir/issues/3580),并且案例内的测试函数似乎是串行运行的。
当我运行一个没有全局状态的简单测试用例时,确实是串行运行的。
但是,当我将全局状态与代理一起使用时,执行顺序取决于运气。对于同一个调用
mix test --trace
Run Code Online (Sandbox Code Playgroud)
执行顺序改变。这是我的测试套件:
defmodule SerialTest do
use ExUnit.Case
test "1" do
Agent.update(:card_id, fn nil -> 1 end)
assert true
end
test "2" do
res = Agent.get(:card_id, fn res -> res end)
assert res == 1
end
test "3" do
Agent.update(:card_id, fn id -> 3 end)
assert true
end
test "4" do
res = Agent.get(:card_id, fn res -> res end)
assert res == 3
end
end
Run Code Online (Sandbox Code Playgroud)
有时通过有时不通过。如何使我的函数的执行顺序符合使用代理的全局状态定义它们的文件中的声明顺序?
我运行一些代码iex -S mix
还行吧:
user = Repo.get User, 1
Ecto.Changeset.change user, %{name: "xxxx"}
Run Code Online (Sandbox Code Playgroud)
但这是错误的:
User.change user, %{name: "xxxx"}
Run Code Online (Sandbox Code Playgroud)
增加(UndefinedFunctionError) undefined function Rumbl.User.change/2
我注意到import Ecto.Changeset文件 web.ex 中有函数模型
def model do
quote do
use Ecto.Schema
import Ecto
import Ecto.Changeset
import Ecto.Query, only: [from: 1, from: 2]
end
end
Run Code Online (Sandbox Code Playgroud)
所以我认为 User.change/2 应该与 Ecto.Changeset.change/2 一样工作,对吗?
我想使用gettexti18n 一个应用程序。我还不明白如何在link. 如何翻译“显示”链接文本及其部分[confirm: "Are you sure?"]?
<%= link "Show", to: user_path(@conn, :show, user), class: "btn btn-default btn-xs" %>
<%= link "Edit", to: user_path(@conn, :edit, user), class: "btn btn-default btn-xs" %>
<%= link "Delete", to: user_path(@conn, :delete, user),
method: :delete, data: [confirm: "Are you sure?"],
class: "btn btn-danger btn-xs" %>
Run Code Online (Sandbox Code Playgroud) 有没有办法在 Elixir 中重构它以使其更具可读性?
def validate(params) do
Repo.start_link
if validate_oauth_params(params) === true do
oauth_client = Repo.get_by(OauthClient, random_id: params["client_id"], secret: params["secret"])
if oauth_client != nil do
allowed_grant_types = Poison.Parser.parse!(oauth_client.allowed_grant_types)
if Map.has_key?(allowed_grant_types, params["grant_type"]) do
case params["grant_type"] do
"password" ->
process_password_grant(params, oauth_client)
"refresh_token" ->
process_refresh_token_grant(params["refresh_token"], oauth_client)
"client_credentials" ->
process_client_credentials_grant(oauth_client)
nil ->
%{message: "Invalid oauth credentials", code: 400}
end
end
else
%{message: "Invalid oauth credentials", code: 400}
end
else
%{message: "Invalid oauth credentials", code: 400}
end
end
Run Code Online (Sandbox Code Playgroud)
Elixir 的做法是什么,因为这段代码看起来像 PHP。不是我写的。
如何使用自定义模型功能来预加载选定的数据Repo.insert?我不会在控制器中复制模型中的代码。
在帖子模型中:
def preload_all(query) do
tags = from(t in Tag, select: %{id: t.id, value: t.id})
from b in query, preload: [:user, tags: ^tags]
end
Run Code Online (Sandbox Code Playgroud)
在控制器中:
case Repo.insert(changeset) do
{:ok, post} ->
# post = post |> Post.preload_all
post = Repo.preload(post, [:user, :tags])
end
Run Code Online (Sandbox Code Playgroud) 背景:
我正在写一个简单的问答网站。与 Stackoverflow 非常相似,在此站点中,用户可以对问题、答案或评论点赞/投票。
问题:
我正在努力编写一个 Ecto 查询,它可以返回用户喜欢/点赞的所有问题、答案或评论。
具体来说,我想编写一个查询:
这个查询似乎可能需要 UNION,据我了解ecto 2.0 尚不支持 UNION。
因此,我想知道是否有人可以向我展示或指出在 Ecto 中解决此类查询的正确方向。感谢您的任何帮助。
以下是相关模型的架构。
...
schema "users" do
...
has_many :answer_upvotes, AnswerUpvote
has_many :comment_upvotes, CommentUpvote
has_many :question_upvotes, QuestionUpvote
many_to_many :upvoted_answers, Answer, join_through: AnswerUpvote
many_to_many :upvoted_comments, Comment, join_through: CommentUpvote
many_to_many :upvoted_questions, Question, join_through: QuestionUpvote
timestamps
end
Run Code Online (Sandbox Code Playgroud)
...
schema "answer_upvotes" do
belongs_to :answer, Answer
belongs_to :user, User
timestamps
end
Run Code Online (Sandbox Code Playgroud)
...
schema "comment_upvotes" do
belongs_to :comment, Comment
belongs_to :user, User
timestamps
end
Run Code Online (Sandbox Code Playgroud)
...
schema "question_upvotes" …Run Code Online (Sandbox Code Playgroud) 寻找 Elixir 方法来编码 uri 组件,即
基本上我想要encode_query,但不必进行键值映射,并将空格编码为 %20,而不是 +javascript
encodeURI("&")
"&"
encodeURIComponent("&")
"%26"
Elixir
URI.encode("&")
"&"
pry(11)> URI.encode_query(%{k: " & "})
"+k=%26+"
找出所有出现的{1, _}; 换句话说,列表中每个元组的所有第一个元素值都是 1。
考虑以下输入:
[
{1, 0},
{2, 2},
{1, 1},
{11, 1},
{1, 3},
{1, 2},
{13, 1}
]
Run Code Online (Sandbox Code Playgroud)
预期输出:
[{1,0}, {1,1}, {1,3}, {1,2}]
Run Code Online (Sandbox Code Playgroud)
我试过了Enum.find(tl(input), fn x -> elem(x, 0) == elem(hd(input), 0) end),但我意识到Enum.find/2只返回第一个也是唯一一个符合条件或函数的元素,即:{1,1}。
我的目标是找到所有包含 的元组{1, _},其中第一个元素必须是 1,第二个元素可以是任何值。