ecto

Elixir ecto 2 create many_to_many association

拥有回忆 提交于 2019-11-30 07:25:52
问题 How can I make a many to many relation with ecto 2? As an example app I want to create a Post which can be in multiple categories. The categories already exist. For example: [%Category{id: "1", name: "elixir"}, %Category{id: "2", name: "erlang"}] Im using Ecto 2 beta 0. The example project is called Ecto2. I defined two models: defmodule Ecto2.Post do use Ecto2.Web, :model use Ecto.Schema schema "posts" do field :title, :string many_to_many :categories, Ecto2.Category, join_through: "posts

Is Phoenix's scrub_params like Rails strong parameters?

♀尐吖头ヾ 提交于 2019-11-30 06:01:49
The documentation for the Phoenix scrub_params method is a little unclear to me. It seems like this functionality is similar to the Rails strong parameters feature. However, when you use it in a controller like so, plug :scrub_params, "user" when action in [:create] ... you're not explicitly stating which parameters you want to whitelist. I've looked at the code for scrub_params , but I'm noobish enough with Elixir that I'm not quite sure what's going on. Is this method just looking at the model and using the required and optional field module attributes for whitelisting parameters? Also, the

Ecto: Order preloaded data in collection with has_many association

非 Y 不嫁゛ 提交于 2019-11-30 04:57:03
问题 Let's say I have this for fetching all threads: Thread |> Thread.ordered |> Repo.all |> Repo.preload([:posts]) How could I provide an order_by clause for :posts ? I can't seem to locate anything in the documentation that references Ecto.Repo.preload/1 , so none of the provided examples appear helpful for figuring out how to use this syntax properly. 回答1: The Ecto.Query module makes it really easy to also apply certain queries to things like preloading. The way we achieve this is by passing a

Why are pin operators necessary in Ecto queries?

只愿长相守 提交于 2019-11-30 04:49:54
In Elixir, the pin operator is used to prevent variable rebinding. However, with regard to an Ecto query like from u in User, where: u.username == ^username the authors of Programming Phoenix state (in chapter 7) that Remember, the ^ operator (called the pin operator) means we want to keep ^username the same. But this doesn't sound right, because apparently, the comparison in the query shall not cause any rebinding of variables. Are the authors of the book (which José Valim co-authored) mistaken? Is the pin operator in Ecto queries merely a construct of the Ecto DSL instead of a usual Elixir

How to change field type in Ecto?

旧时模样 提交于 2019-11-30 04:43:05
I have a schema: schema "editables" do field :title, :string field :content, :string timestamps end Now I want to change the type of one field form :integer to :binary . What's the correct way of writing the migration because using add is not working...? def change do alter table(:editables) do add :title, :binary add :content, :binary timestamps end end You have to use modify/3 to change the type. add/3 is only for adding new columns. alter table(:editables) do modify :content, :binary end 来源: https://stackoverflow.com/questions/32583625/how-to-change-field-type-in-ecto

Can I get Ecto to log raw SQL?

拜拜、爱过 提交于 2019-11-29 15:37:24
I am building an Ecto query like this: from item in query, where: like(item.description, ^"%#{text}%") I'm concerned that this allows SQL injection in text . Before trying to fix that, I want to see how the query is actually sent to the database. If I inspect the query or look at what is logged, I see some SQL, but it's not valid. For instance, inspecting the query shows me this: {"SELECT i0.\"id\", i0.\"store_id\", i0.\"title\", i0.\"description\" FROM \"items\" AS i0 WHERE (i0.\"description\" LIKE $1)", ["%foo%"]} When I pass this query to Repo.all , it logs this: SELECT i0."id", i0."store

Dynamic Models in Phoenix Framework

别说谁变了你拦得住时间么 提交于 2019-11-29 14:38:47
问题 Is there any way to dynamically create and use models in Phoenix? I have an application that stores metadata about clients' tables: they set a handful of fields (column names and types) and then send me CSV files to parse and store. From the stored metadata, I'd like to generate a model so that I can use Ecto to manage the client table and perform queries against it. I'm coming from a Django background where I can use the built in ORM and type() function to build models on the fly and then

Ecto 2.0 SQL Sandbox Error on tests

一个人想着一个人 提交于 2019-11-29 06:55:12
I recently upgraded my phoenix project to Ecto 2.0.2. I have some code that is using Task.Supervisor.async_nolink to make some updates to the db on its own thread. I am getting the following error when my tests run (only occurs on my tests) [error] Postgrex.Protocol (#PID<0.XXX.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.XXX.0> exited while client #PID<0.XXX.0> is still running with: shutdown Now I think I understand whats happening: The Ecto Sandbox connection pool is being checked back in before the db transaction is complete. According to the docs (at least the way I

Change Url to accept a string instead id in phoenix framework (elixir)

假如想象 提交于 2019-11-29 06:24:22
I am trying to implement permalinks in phoenix app. The goal is to change localhost:4000/products/1 to localhost:4000/products/productname I tried following Ryan Bates episode on permalinks implementation in rails but wasn't able to find a to_param function for models in phoenix. Please help. Not sure if this is what you are asking for but here you go: router.ex in the browser stack get "/products/:product_name", ProductController, :get_product_by_name product_controller.ex def get_product_by_name(conn, %{"product_name" => product_name}) do product = Repo.get_by(Product, name: product_name)

Is Phoenix's scrub_params like Rails strong parameters?

坚强是说给别人听的谎言 提交于 2019-11-29 05:19:11
问题 The documentation for the Phoenix scrub_params method is a little unclear to me. It seems like this functionality is similar to the Rails strong parameters feature. However, when you use it in a controller like so, plug :scrub_params, "user" when action in [:create] ... you're not explicitly stating which parameters you want to whitelist. I've looked at the code for scrub_params , but I'm noobish enough with Elixir that I'm not quite sure what's going on. Is this method just looking at the