3
我有一個User
模型,has_many
與other_model
的關係。 我有一個功能Search
搜索互聯網。我只想在has_many
關係不是空數組的情況下搜索互聯網。你能模式匹配elixir中的一個非空數組嗎?
所以我想知道如果我可以模式匹配一個非空數組?正如你在下面看到的,額外的Search
導致嵌套分支,因此我使用with
語句,並希望有一個乾淨的解決方案。
query = from a in Model, where: a.id == ^id, preload: [:some_associations]
with %{some_associations: some_associations} <- Repo.one(query),
{:ok, some_results} <- Search.call(keywords, 1) do
do_something_with(some_associations, some_results)
else
nil -> IO.puts "query found nothing"
{:error, reason} -> IO.puts "Search or query returned error with reason #{reason}"
end
你的意思是像'{:OK,some_results = [_ | _]} < - ...'? – Dogbert
我需要some_associatuons不爲空。嗯,我會嘗試,當我回家。沒有意識到你可以做到這一點...... –
@Dogbert你需要發佈一些作爲答案:)人們會在評論中想念他們。 :) –