Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Elixir 长生不老药&x2B;健忘症:如何读取属性?_Elixir_Phoenix Framework - Fatal编程技术网

Elixir 长生不老药&x2B;健忘症:如何读取属性?

Elixir 长生不老药&x2B;健忘症:如何读取属性?,elixir,phoenix-framework,Elixir,Phoenix Framework,我正在尝试使用此十六进制软件包: 以下是我的模式: use Amnesia require Logger require IEx defdatabase Database do deftable User, [{ :id, autoincrement }, :device_identifier, :match_history], type: :ordered_set, indices: [:device_identifier] do @type t :: %User{ id: non

我正在尝试使用此十六进制软件包:

以下是我的模式:

use Amnesia
require Logger
require IEx
defdatabase Database do
  deftable User, [{ :id, autoincrement }, :device_identifier, :match_history], type: :ordered_set, indices: [:device_identifier] do
    @type t :: %User{ id: non_neg_integer, device_identifier: String.t, match_history: List.t }

    ...
  end
end
下面是错误发生的地方:

Amnesia.transaction do
  // IEx.pry
  queue = User.where(:device_identifier != device_identifier)
  if queue do
    Logger.info "dequeuing another user"
    matched_user = List.first(queue.values)
    matched_user |> User.delete
    push socket, "match_found", %{ device_identifier: matched_user.device_identifier } 
  else
    Logger.info "queuing user"
    push socket, "queued", %{}
  end
end
当我尝试调用
匹配的\u user.device\u identifier
时,它失败了。但这不是模型上的一个属性吗?在github repo的自述文件中,它调用消息模型上的
内容
,那么为什么我不能对我的用户模型上的
设备标识符
执行相同的操作呢

我甚至用
id
字段进行了尝试:

pry(8)> matched_user.id
** (UndefinedFunctionError) function Database.User.id/1 is undefined or private. Did you mean one of:

      * id/0

有没有办法找出匹配的用户对象响应的方法?

默认情况下,健忘症会为每个匹配项返回一个元组,而不是相应的结构,例如,在您的示例中,它返回
{Database.user,2,“5pyia9cej4”,nil}
。为了从原始查询结果中获取结构,健忘症提供了一个方便的函数
健忘症.Selection.values/1
。以下改变应该有效:

替换

matched_user = List.first(queue.values)

您可能还希望在此处使用模式匹配,以使代码更加地道:

[matched_user | _] = Amnesia.Selection.values(queue)

@Edmund在关于如何更新现有记录的评论中回答了您的问题。无法格式化注释中的代码,因此我发布了一个答案。我突然意识到了这一点。下面是一个小函数,它似乎在做如何更新记录的诀窍。首先,必须选择需要更新的记录。我对长生不老药完全陌生,但我认为它并没有那么糟糕:

def update_value(entity_id) do
  Amnesia.transaction do
    entity = Entity.where entity_key == entity_id
  end
  |> Amnesia.Selection.values
  |> Enum.each(fn e ->
    Amnesia.transaction do
      %Entity{e | status: :started} |>
      Entity.write()
    end
  end) 
end

在执行
matched\u user.id
之前,你能试着打印
matched\u user.id
的值吗?@Dogbert yeh我得到:
{Database.user,2,“5pyia9cej4”,nil}
你知道我将如何
更新现有记录吗?我在这些测试中找不到任何文档…我在测试中找到了
健忘症.Selection.values
。只有当数据库类型为
set
时,它才起作用。如果数据库是一个
,则会创建一条新记录
def update_value(entity_id) do
  Amnesia.transaction do
    entity = Entity.where entity_key == entity_id
  end
  |> Amnesia.Selection.values
  |> Enum.each(fn e ->
    Amnesia.transaction do
      %Entity{e | status: :started} |>
      Entity.write()
    end
  end) 
end