Get current environment name

Mix.env() doesn’t work in production or other environments where you use compiled releases (built using Exrm / Distillery) or when Mix just isn’t available. The solution is to specify it in your config/config.exs file: config :your_app, env: Mix.env() You can then get the environment atom in your application like this: Application.get_env(:your_app, :env) #=> :prod Update … Read more

Elixir: How to get last n items in a list?

Use Enum.take/2 with a negative value: iex(1)> list = [1, 2, 4, 5, 6, 7, 8, 9, 9, 88, 88] iex(2)> Enum.take(list, -4) |> IO.inspect(charlists: :as_lists) [9, 9, 88, 88] take(enumerable, count) […] count must be an integer. If a negative count is given, the last count values will be taken. […]

Get Random Element(s) from a List

Updated Answer As José Valim said in his answer, in Elixir 1.1 and above, you can now use these methods to get random element(s) from a list: Enum.random/1 – For getting single random element Enum.take_random/2 – For getting multiple random elements Example: Enum.random(list) #=> 4 Enum.take_random(list, 3) #=> [3, 9, 1] Enum.take_random(list, 1) #=> [7] … Read more

Modify foreign key in Ecto

I’m not sure when this was added to Ecto, but at least in 2.1.6 there’s no need for raw SQL anymore. drop/1 now supports constraints (drop_if_exists/1 doesn’t though): def up do drop constraint(:videos, “videos_user_id_fkey”) alter table(:videos) do modify :user_id, references(:users, on_delete: :delete_all) end end def down do drop constraint(:videos, “videos_user_id_fkey”) alter table(:videos) do modify :user_id, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)