ecto

Conditional Validation in Ecto for OR - 1 of 2 fields is required

◇◆丶佛笑我妖孽 提交于 2019-12-06 00:28:11
问题 How can I do conditional validation for OR logic, where we check to see if 1 of the 2 values is present or both values are present. So, for example, if I want to check to make sure that the email or the mobile fields are filled... I want to be able to pass a list into fields of validate_required_inclusion to validate that at least 1 of the fields in the list is not null. def changeset(struct, params \\ %{}) do struct |> cast(params, [:email, :first_name, :last_name, :password_hash, :role,

Phoenix: Ordering a query set

穿精又带淫゛_ 提交于 2019-12-05 13:53:02
I'm [a noob] playing around with the Phoenix framework for fun and building a small twitter clone. I everything working, however, I want to order the tweets by the updated_at field (ascending). As you can see from tweet_controller, I have tried messing around with an order_by clause, but this did nothing for me. Question How do I achieve this? Within the EEx or within the tweet_controller itself? tweet/index.html.eex <div class="row"> <%= for tweet <- @tweets do %> <h4><%= tweet.tweet %></h4> <% end %> </div> controllers/tweet_controller.ex ... alias TodoApp.Tweet def index(conn, _params) do

Ecto: How to preload records with selecting another joined columns

白昼怎懂夜的黑 提交于 2019-12-05 11:57:24
Are there any ways to preload records by selecting another joined columns? # table structure # User 1---* Post 1---* PostTag *---1 Tag # extract definition of scheme scheme "posts" do ... has_many :post_tags, PostTag has_many :tags, [:post_tags, :tag] end Following pseudo-code expresses my goal(but not work). query = from post in Post, join: user in User, on post.user_id == user.id, select: %{ id: post.id, title: post.title, user_name: user.name, # <= column at joined table }, preload: [:tags] Repo.all(query) #=> ** (Ecto.QueryError) the binding used in `from` must be selected in `select` when

Ecto has_many :through in form

妖精的绣舞 提交于 2019-12-05 09:05:32
I am trying to get a has_many :through relationship working in Ecto for a many-many relationship between a User model and Group model. The only information I was able to find online was related to nested attributes in a post by José Valim here (which is excellent, by the way). Since the groups already exist in the system, I was hoping to do a multiple select input. I ran into a couple issues in doing that. I don't believe that it is possible to assign the groups association in the changeset directly because I was getting an error every time I attempted to do so. My next thought was to do the

How to use Geo library to create valid Ecto Model changeset?

主宰稳场 提交于 2019-12-05 05:54:42
I'm trying to use Geo library to store Geo.Point via Phoenix model changeset. My params are: {coordinates: [49.44, 17.87]} or more prefer would be {coordinates: {latitude: 49.44, longitude: 17.87}} In iex console I tried: iex(5)> changeset = Place.changeset(%Place{}, %{coordinates: [49.44, 17.87]}) %Ecto.Changeset{action: nil, changes: %{}, constraints: [], errors: [coordinates: "is invalid"], filters: %{} model: %Myapp.Place{__meta__: #Ecto.Schema.Metadata<:built>, coordinates: nil, id: nil, inserted_at: nil, updated_at: nil}, optional: [], opts: [], params: %{"coordinates" => [49

Ecto Query - Dates + Postgres Intervals + Query Interpolation

一笑奈何 提交于 2019-12-05 05:43:36
问题 I would like to create an Ecto query that filters records in a children table by their age (i.e. "minimum age (months) -> maximum age (months)". One simple way to do this would be the Ecto date_add feature: from c in Child, where: c.birthday > datetime_add(^Ecto.DateTime.utc, -1, "month") The issue with this is that not all children will be on the same time zone, and certainly not all on Etc/UTC . This query would be pretty close, but not spot on (some would be off by a day). I've been trying

Adding timestamps to an existing table with Ecto's timestamps

巧了我就是萌 提交于 2019-12-05 04:19:26
This has been asked here How to add timestamps to an existing table with Ecto's timestamps? , however the accepted solution means that every new entry will have the same default time. I would want the new entries to have the correct time of insert/update. eg. # set default to given date to fill in all existing rows with timestamps def change do alter table(:my_table) do timestamps(default: "2018-01-01 00:00:01") end end If this is all that is in the migration, every inserted_at and updated_at for :my_table will have 2018-01-01 00:00:01 as the value, regardless of the date it was inserted

cannot invoke remote function inside match : Foreach loop

╄→гoц情女王★ 提交于 2019-12-05 03:44:11
I'm trying to set some property of User model inside a for-each loop, But I keep getting following error cannot invoke remote function x.token/0 inside match (elixir) src/elixir_fn.erl:9: anonymous fn/3 in :elixir_fn.translate/3 (stdlib) lists.erl:1353: :lists.mapfoldl/3 (elixir) src/elixir_fn.erl:14: :elixir_fn.translate/3 Method: Enum.each(users, fn(user) -> user.token = Comeonin.Bcrypt.hashpwsalt(to_string(user.id)) end) There are a few issues here. The = operator is the match operator, it is not assignment. To explain the error, syntax-wise, this looks like function invocation on the left

Default datetime with Ecto & Elixir

蹲街弑〆低调 提交于 2019-12-05 03:31:30
I've just started working Elixir & Phoenix today, i am trying to add Ecto as a mapper, but i'm having some trouble using time. This is my model. schema "users" do field :name, :string field :email, :string field :created_at, :datetime, default: Ecto.DateTime.local field :updated_at, :datetime, default: Ecto.DateTime.local end I'm trying to set the created_at and updated_at per default, but when i try to compile this, i get the following error. == Compilation error on file web/models/user.ex == ** (ArgumentError) invalid default argument `%Ecto.DateTime{day: 13, hour: 19, min: 47, month: 2, sec

Setting up custom response for exception in Phoenix Application

不想你离开。 提交于 2019-12-05 00:05:51
im writing phoenix application with ecto and have the following snippet in the test {:ok, data} = Poison.encode(%{email: "nonexisting@user.com", password: "mypass"}) conn() |> put_req_header("content-type", "application/json") |> put_req_header("accept", "application/json") |> post(session_path(@endpoint, :create), data) > json_response(:not_found) == %{} this throws a Ecto.NoResultsError i have this defined defimpl Plug.Exception, for: Ecto.NoResultsError do def status(_exception), do: 404 end but the test still throws Ecto.NoResultsError, Any pointers? Let's consider how it works per