venomnert

venomnert

Many to many relationship

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.

Most Liked

csisnett

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

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 migration

 create table(:customerlead_service) do
      add :service_category_id, references("service_categories")
      add :customer_lead_id, references("customer_leads")
    end

However, I’m not sure as to why the schema was causing this issue though.

Also this post at ElixirSchool definitely helped:

csisnett

csisnett

Sure no problem @venomnert,

you would need a changeset function for the customerlead_service schema as well.

def changeset(customerlead_service, attrs) do
  customerlead_service
  |> cast(attrs, [:customer_leads_id, :service_categories_id])
  |> validate_required([:customer_leads_id, :service_categories_id])
end

To create the record you would need the parent tables to be created already


customer_lead = %CustomerLead{...}
service_category = %ServiceCategory{...}
attrs = %{"customer_leads_id" => customerlead.id, "service_categories_id" => service_category.id}

and call this function (which should be in your Context) passing attrs

def create_customerlead_service(attrs = %{}) do
  %CustomerLeadService{}
  |> CustomerLeadService.changeset(attrs)
  |> Repo.insert()
end

Not relevant to the question but I would change field names in the customerlead_service schema from :customer_leads and :service_categories to :customer_lead and service_category as you’re only referring to one and not several in the intermediary table.

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
JDanielMartinez
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
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New

We're in Beta

About us Mission Statement