How can I see the raw SQL generated for an Ecto.Query?

You can use Ecto.Adapters.SQL.to_sql/3: iex> Ecto.Adapters.SQL.to_sql(:all, Repo, Post) {“SELECT p.id, p.title, p.inserted_at, p.created_at FROM posts as p”, []} This function is also available under the repository with name to_sql if you’re using a SQL based adapter: iex> Repo.to_sql(:all, Post) {“SELECT p.id, p.title, p.inserted_at, p.created_at FROM posts as p”, []} The query can be any struct … Read more

How to use raw sql with ecto Repo

On Ecto 2.0 (beta) with Postgres, you can use Ecto.Adapters.SQL.query() (current docs, 2.0-beta2 docs) to execute arbitrary SQL; in addition to a list of the rows themselves (“rows“), it happens to return a list of column names (“columns“). In the below example, I run a custom query with no parameters, convert the result’s column names … Read more

Difference between protocol & behaviour in elixir

Protocol is type/data based polymorphism. When I call Enum.each(foo, …), the concrete enumeration is determined from the type of foo. Behaviour is a typeless plug-in mechanism. When I call GenServer.start(MyModule), I explicitly pass MyModule as a plug-in, and the generic code from GenServer will call into this module when needed.

How to connect two Elixir nodes via local network?

You have to name your nodes and use the same cookie on both nodes. In machine 1: iex –name node1@machine1.com –cookie a_cookie_string In machine 2: iex –name node2@machine2.com –cookie a_cookie_string Now the two machines can communicate. To test it, you can do something like this, on machine1: iex(node1@machine1.com)1> Node.connect :”node2@machine2.com” true iex(node1@machine1.com)2> print_node_name = fn … Read more

Lists vs. Tuples – What to use and when?

You’ve already given a pretty good summary of the differences, so in any conditions where one of those things is important it should help you decide on which to use. The way to think about it is that Lists are open-ended data structures and their size can vary at runtime while Tuples have a constant … Read more

How to read config variable in Phoenix / Elixir?

The get_env function is part of the Application module from the Elixir/Erlang core. This function returns the value for a specific key in the app’s environment. Considering your configuration, you would access the title property this way: Application.get_env(:my, My.Endpoint)[:title] The third parameter is for passing a default value when the config key doesn’t exist.

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