venomnert
Context:
A customer_lead can be associated to many service_category. A service_category can be associated to many customer_leads.
customerleads_service migration
create table(:customerlead_service) do
add :service_category_id, references("service_categories")
add :customer_lead_id, references("customer_leads")
timestamps()
end
customer_service schema
schema "customerlead_service" do
belongs_to :customer_leads, CustomerLead
belongs_to :service_categories, ServiceCategory
timestamps()
end
customer migration
create table(:customer_leads) do
add :first_name, :string
add :last_name, :string
add :email, :string
add :address, :string
add :phone_number, :string
add :company_name, :string
add :message, :string
add :status, :boolean, null: false, default: false
timestamps()
end
customer schema
schema "customer_leads" do
field :first_name, :string
field :last_name, :string
field :email, :string
field :address, :string
field :phone_number, :string
field :company_name, :string
field :message, :string
field :status, :boolean
many_to_many :service_categories, ServiceCategory,
join_through: CustomerleadService
timestamps()
end
def changeset(customer, attrs \\ %{}) do
customer
|> cast(attrs, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
end
def add_service_to_customer_lead(changeset, attrs) do
service = Ramlaw.Services.get_service_category_by([slug: attrs["services"]])
|> Repo.preload(:customer_leads)
changeset
|> Repo.preload(:service_categories)
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_assoc(:service_categories, [service])
|> Repo.update!()
end
service migration
create table(:service_categories) do
add :title, :string
add :slug, :string
add :status, :boolean, null: false, default: false
timestamps()
end
service schema
schema "service_categories" do
field :title, :string
field :slug, :string
field :status, :boolean
has_many :services, Service,
on_delete: :delete_all,
foreign_key: :service_categories_id,
on_replace: :nilify
many_to_many :customer_leads, CustomerLead,
join_through: CustomerleadService
timestamps()
end
The problem
In my test I noticed when I first create a customer_lead, the service_category get’s associated properly
AFTER INSERT: %Ramlaw.Schema.CustomerLead{
__meta__: #Ecto.Schema.Metadata<:loaded, "customer_leads">,
address: nil,
company_name: "profilo",
email: "venomnert1994@hotmail.com",
first_name: "Nert",
id: 4,
inserted_at: ~U[2019-11-10 13:23:06Z],
last_name: "test",
message: "fdafsa",
phone_number: "6473355012",
service_categories: [
%Ramlaw.Schema.ServiceCategory{
__meta__: #Ecto.Schema.Metadata<:loaded, "service_categories">,
customer_leads: [],
id: 10,
inserted_at: ~U[2019-11-10 13:23:06Z],
services: #Ecto.Association.NotLoaded<association :services is not loaded>,
slug: "family-law",
status: false,
title: "Family Law",
updated_at: ~U[2019-11-10 13:23:06Z]
}
],
status: nil,
updated_at: ~U[2019-11-10 13:23:06Z]
}
However, when I query the same customer_lead that was added, its service_category is empty. Also this applies to the associated service_category as well.
Code snippet
service_cat
|> Repo.preload(:customer_leads)
|> IO.inspect(label: "RESULTS")
ClientLeads.get_client_lead(added_client.id)
|> IO.inspect(label: "TEST")
result
RESULTS: %Ramlaw.Schema.ServiceCategory{
__meta__: #Ecto.Schema.Metadata<:loaded, "service_categories">,
customer_leads: [],
id: 10,
inserted_at: ~U[2019-11-10 13:23:06Z],
services: #Ecto.Association.NotLoaded<association :services is not loaded>,
slug: "family-law",
status: nil,
title: "Family Law",
updated_at: ~U[2019-11-10 13:23:06Z]
}
TEST: %Ramlaw.Schema.CustomerLead{
__meta__: #Ecto.Schema.Metadata<:loaded, "customer_leads">,
address: nil,
company_name: "profilo",
email: "venomnert1994@hotmail.com",
first_name: "Nert",
id: 4,
inserted_at: ~U[2019-11-10 13:23:06Z],
last_name: "test",
message: "fdafsa",
phone_number: "6473355012",
service_categories: [],
status: false,
updated_at: ~U[2019-11-10 13:23:06Z]
}
I feel like I have set up everything correctly regarding the many-to-many association; however, i’m not able to retrieve the associations.
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
venomnert
If anyone else runs into this issue, the solution is to remove schema for the joining table
customer_service schema.And update your schema to the following, which is to remove the timestamp:
customerleads_service migrationHowever, I’m not sure as to why the schema was causing this issue though.
Also this post at ElixirSchool definitely helped:
csisnett
timestamps() don’t work when you create an intermediary record like that. You have to create a new record independently by passing the parents tables IDs as attributes. The problem I’ve had with the update syntax as shown in the elixir school article is that you’re replacing all intermediary records customer_lead_services with those specific customer_lead_id and service_categories_id, so it’s not the best option to use the syntax if you want to continue to add intermediary records afterward.
venomnert
Hey @csisnett sorry for the late response. But can you elaborate more on this by providing some pseudo code. I’m not able to follow what you are saying, i’m still new to ecto
csisnett
Sure no problem @venomnert,
you would need a changeset function for the customerlead_service schema as well.
To create the record you would need the parent tables to be created already
and call this function (which should be in your Context) passing
attrsNot relevant to the question but I would change field names in the
customerlead_serviceschema from:customer_leadsand:service_categoriesto:customer_leadandservice_categoryas you’re only referring to one and not several in the intermediary table.venomnert
@csisnett with this current approach as you mentioned initially “You have to create a new record independently by passing the parents tables IDs as attributes”, I would manually have to create the record, so that timestamp works.
Out of curiosity, is there a better alternative to creating many-to-many relationship?