ecto

Elixir ecto connect to an existing DB

ⅰ亾dé卋堺 提交于 2020-01-13 13:04:21
问题 After doing some research in the link below https://github.com/elixir-lang/ecto/tree/master/examples/simple I am a little confused about how to use ecto in elixir. There is always a schema declared like defmodule Weather do use Ecto.Model schema "weather" do field :city, :string field :temp_lo, :integer field :temp_hi, :integer field :prcp, :float, default: 0.0 timestamps end end and then in the 'Query' part def sample_query do query = from w in Weather, where: w.prcp > 0.0 or is_nil(w.prcp),

Elixir ecto connect to an existing DB

老子叫甜甜 提交于 2020-01-13 13:04:13
问题 After doing some research in the link below https://github.com/elixir-lang/ecto/tree/master/examples/simple I am a little confused about how to use ecto in elixir. There is always a schema declared like defmodule Weather do use Ecto.Model schema "weather" do field :city, :string field :temp_lo, :integer field :temp_hi, :integer field :prcp, :float, default: 0.0 timestamps end end and then in the 'Query' part def sample_query do query = from w in Weather, where: w.prcp > 0.0 or is_nil(w.prcp),

Make Ecto queries more efficient

本秂侑毒 提交于 2020-01-04 14:05:26
问题 I'm trying to see if my current user's teams overlap with the passed in user's teams. I have something that works but I'm curious if it could me more efficient. Here is what I have: user_teams = from( t in MyApp.Team, left_join: a in assoc(t, :accounts), where: p.owner_id == ^user.id or (a.user_id == ^user.id and t.id == a.project_id) ) |> Repo.all current_user_teams = from( t in MyApp.Team, left_join: a in assoc(t, :accounts), where: t.owner_id == ^current_user.id or (a.user_id == ^current

Elixir + Ecto: How to do WHERE NOT IN [array]?

帅比萌擦擦* 提交于 2020-01-03 08:09:01
问题 I am trying to look for all User s that don't have a certain string element in their match_history field. I took a guess with this: matched_user = User |> where([u], ^device_id not in u.match_history) |> limit(1) |> VideoChat.Repo.one But it seems to break at the not part. Is there a way to do this? 回答1: Try User |> where([u], not ^device_id in u.match_history) 回答2: For those who are looking for a "array does not contain any" behaviour. For example "match_history does not contain device_1

Elixir + Ecto: How to do WHERE NOT IN [array]?

南楼画角 提交于 2020-01-03 08:06:09
问题 I am trying to look for all User s that don't have a certain string element in their match_history field. I took a guess with this: matched_user = User |> where([u], ^device_id not in u.match_history) |> limit(1) |> VideoChat.Repo.one But it seems to break at the not part. Is there a way to do this? 回答1: Try User |> where([u], not ^device_id in u.match_history) 回答2: For those who are looking for a "array does not contain any" behaviour. For example "match_history does not contain device_1

How to update field value dynamically in Ecto migration?

时光怂恿深爱的人放手 提交于 2020-01-03 03:02:06
问题 I have a Users table like: email | username ---------------+---------- 123@321.com | 123@123.com | haha@haha.com | and I want to update username field by email field, just slice the email before @ . email | username ---------------+---------- 123@321.com | 123 123@123.com | 123 haha@haha.com | haha I have try to use the following migration: defmodule MyApp.Repo.Migrations.AddDefaultUsernameForUsers do use Ecto.Migration import Ecto.Query def up do from(u in MyApp.User, update: [set: [username

Ecto has_many :through in form

非 Y 不嫁゛ 提交于 2020-01-02 03:33:45
问题 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

How to handle associations and nested forms in Phoenix framework?

泄露秘密 提交于 2019-12-31 10:07:47
问题 What is the way to handle associations and nested forms in Phoenix framework? How would one create a form with nested attributes? How would one handle it in the controller and model? 回答1: There is a simple example of handling 1-1 situation. Imagine we have a Car and an Engine models and obviously a Car has_one Engine . So there's code for the car model defmodule MyApp.Car do use MyApp.Web, :model schema "cars" do field :name, :string has_one :engine, MyApp.Engine timestamps end def changeset

Elixir/Phoenix restrict params like Rails strong params

眉间皱痕 提交于 2019-12-31 02:23:06
问题 I am making an API only Phoenix app. I come from a Ruby on Rails background, so bear with me. Say I have a User model with email , password , password_hash , and role fields. I need to restrict the role and password_hash fields from user input, or whitelist the email and password fields. Right now anyone could POST this sign up as an admin: { "user": { "email": "test3@test.com", "password": "testpw", "password_hash": "shouldn't allow user input", "role": "admin" } } This is typically

Poison.Encoder how to preload associations?

我们两清 提交于 2019-12-24 00:59:58
问题 I have the Ecto model below. When I try to render I get an error. How can I modify the @derive so it will preload? Or do I have to write out the implementation? What is the recommended way of dealing with this? ** (RuntimeError) cannot encode association :tilemap_layers from MyProject.Tilemap to JSON because the association was not loaded. Please make sure you have preloaded the association or remove it from the data to be encoded The model is here: defmodule MyProject.Tilemap do use