tnederlof
Removing an Association with Ecto
I have an Event that has_one Number (the schema and migrations are shown simplified below). I can use put_assoc when inserting a number to associate a number with an event. However I would like to be able to disassociate the two (essentially remove the relationship) without deleting the Number or Event (so the number would end up with no event_id). Any ideas would be quite helpful.
defmodule App.Events.Event do
use Ecto.Schema
import Ecto.Changeset
schema "events" do
field :description, :string
field :name, :string
has_one :number, App.Events.Number
timestamps()
end
@doc false
def changeset(event, attrs) do
required_fields = [:name]
optional_fields = [:description]
event
|> cast(attrs, required_fields ++ optional_fields)
|> validate_required(required_fields)
end
end
defmodule App.Repo.Migrations.CreateEvents do
use Ecto.Migration
def change do
create table(:events) do
add :name, :string
add :description, :text
timestamps()
end
end
end
defmodule App.Events.Number do
use Ecto.Schema
import Ecto.Changeset
schema "numbers" do
field :description, :string
field :name, :string
field :phone_number, :string
belongs_to :event, App.Events.Event
timestamps()
end
@doc false
def changeset(number, attrs) do
required_fields = [:name, :phone_number]
optional_fields = [:description]
number
|> cast(attrs, required_fields ++ optional_fields)
|> validate_required(required_fields)
end
end
defmodule App.Repo.Migrations.CreateNumbers do
use Ecto.Migration
def change do
create table(:numbers) do
add :name, :string
add :description, :text
add :phone_number, :string, null: false
add :event_id, references(:events, on_delete: :delete_all)
timestamps()
end
end
end
Most Liked
idi527

Would setting the event_id to NULL work?
import Ecto.Query
def disassociate_number_from_event(number_id) do
App.Events.Number
|> where(id: ^number_id)
|> App.Repo.update_all(set: [event_id: nil])
end
3
Popular in Questions
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
In Ruby, I can go:
User.find_by(email: "foobar@email.com").update(email: "hello@email.com")
How can I do something similar in Elixir? ...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
In AR this is so simple
@articles = current_user.articles
How to do in Ecto?
def index(conn, _params) do
current_user = conn.assig...
New
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir
iex(2)...
New
Other popular topics
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New







