elixir

function nil.id/0 is undefined or private - Elixir

北城以北 提交于 2019-12-10 19:40:57
问题 I'm trying to test if a user id is equal to the resource id using guardian auth. If there is no current token and I try to go to a url that is checking for a token I get this error function nil.id/0 is undefined or private . I come from a ruby background and I don't know why it's saying .id is a function? and why this is throwing an error. Here is my code: def index(conn, %{"user_id" => user_id}) do user = Repo.get(User, user_id) |> Repo.preload(:projects) cond do user.id == Guardian.Plug

group or count duplicated letters in elixir

爷,独闯天下 提交于 2019-12-10 18:42:26
问题 I'm trying to count duplicated letters on a String in Elixir. I did try some attempts, but no success until now. Let's take this string as example: "AAABBAAC" The desired output would be "3A2B2A1C" . Converting this string to a List , I was able to count every letter, resulting in "5A2B1C" , but I have to count following the order. This is the code I was doing: string |> String.graphemes |> Enum.reduce([], fn(letter, acc) -> Keyword.update(acc, letter, 1, &(&1 + 1)) end) But, in my tests, I'm

Elixir list concatenation

社会主义新天地 提交于 2019-12-10 18:23:12
问题 So I've been playing with Elixir and am a bit confused about something: iex> [ 1 | [ 2 ] ] // [ 1, 2] (expected) iex> [ 1 | 2 ] // [ 1 | 2 ] (huh?) My confusion is in why the second version does what it does. I understand that 2 is not a list, so it can't concatenate the "head" with the "tail", but, in my opinion, it should throw an error when the tail is not a list. I've been trying to think of a use-case for having this behavior but have come empty-handed. If anyone can explain why this is

get_in for nested list & struct in elixir

て烟熏妆下的殇ゞ 提交于 2019-12-10 17:57:28
问题 I have a structure s = [ a: %Bla{ b: "c" } ] I want to take c value from it. I'm trying to do get_in(s, [:a, :b]) But it's not designed to take value from the struct. Is there any analogue which allows me to fetch c from the list with nested struct? 回答1: As documented, get_in does not work with structs by default: The Access syntax (foo[bar]) cannot be used to access fields in structs, since structs do not implement the Access behaviour by default. It is also design decision: the dynamic

Ecto “left IN right” query using a fragment

纵饮孤独 提交于 2019-12-10 17:46:47
问题 I would like to query a jsonb field using postgres IN operator (with Ecto library) This code work with a simple = operator: from a in query, where: fragment("?->>'format' = ?", a.properties, "foo") But I cannot make any of these attempts to work: from a in query, where: fragment("?->>'format' IN ?", a.properties, ["foo", "bar"]) from a in query, where: fragment("?->>'format' IN (?)", a.properties, ["foo", "bar"]) from a in query, where: fragment("?->>'format' IN ?", a.properties, "('foo',

How to manipulate regex replacement strings in Elixir

不打扰是莪最后的温柔 提交于 2019-12-10 17:44:16
问题 I found myself wanting to do this in Elixir: re_sentence_frag = %r/(\w([^\.]|\.(?!\s|$))*)(?=\.(\s|$))/ Regex.replace(re_sentence_frag, " oh. a DOG. woOf. ", String.capitalize("\\1")) Of course, that has no effect. (It capitalizes the string "\\1" just once.) What I really meant is to apply String.capitalize/1 to every match found by the replace function. But the 3rd parameter can't take a function reference, so passing &(String.capitalize("\\1") also doesn't work. This seems so fundamental

Broadcast to different channel Phoenix 1.1.6

六月ゝ 毕业季﹏ 提交于 2019-12-10 16:45:12
问题 I'm trying to broadcast to a different channel in my app but I can not get it to work. Also I trying to write a test but I'm not sure how. From what I can gather I succeed in broadcasting the message from the notification_channel but it's not received in chat_channel. Notification should send to chat. notification_channel.ex def handle_in("new:group:recommendation", msg, socket) do payload = %{ message: msg["message"], url: msg["url"], title: msg["title"], user_name: get_name_of_user(socket

Why is data sharing in Elixir / Erlang not working for the same tuple in two maps

拥有回忆 提交于 2019-12-10 16:44:33
问题 Given the following code defmodule Test do def run do p1 = {1, 2} m1 = %{a: p1} m2 = %{a: p1} IO.puts :erts_debug.same(m1.a, m2.a) m3 = %{b: p1} IO.puts :erts_debug.same(m1.a, m3.b) end end why does Test.run prints this iex(1)> Test.run true <--- expected false <--- not true ?! :ok Why are m1.a and m3.b not the same in-memory tuple? 回答1: modern era update: seems like it was fixed in ≈v1.7. This is true for Elixir only; in Erlang the tuple is shared: 1> Tuple = {1, 2}, 1> Key1 = 1, 1> Key2 = 2

Fix protocol Ecto.Queryable not implemented error

烂漫一生 提交于 2019-12-10 16:33:25
问题 I'm new to using Ecto and Elixir and I've come across an error that I can't explain. My code looks just like the example in the Ecto README. Here are my modules for the Ecto Model and Query defmodule Registration do use Ecto.Model schema "registrations" do field :user_id, :string field :created_at, :datetime, default: Ecto.DateTime.local field :updated_at, :datetime, default: Ecto.DateTime.local end end defmodule RegistrationQuery do import Ecto.Query def by_user(user_id) do query = from r in

Bad value on output port 'tcp_inet'

六月ゝ 毕业季﹏ 提交于 2019-12-10 16:06:20
问题 I'm using the Phoenix framework, which is running Cowboy underneath. I am occasionally seeing the following pair of errors in my log: Bad value on output port 'tcp_inet' GenServer #PID<0.8423.1> terminating ** (FunctionClauseError) no function clause matching in :http_transport.close/2 (inets) http_transport.erl:346: :http_transport.close(:undefined, #Port<0.18079778>) (stdlib) gen_server.erl:643: :gen_server.try_terminate/3 (stdlib) gen_server.erl:809: :gen_server.terminate/7 (stdlib) proc