wernerlaude

wernerlaude

How to get user_id in the 'belongs_to' table

I have a schema:

defmodule Anders.Projects.Angebot do
 use Ecto.Schema
 import Ecto.Changeset

 schema "angebots" do
   ....
 field :user_id, :id

and a controller living in > anders_web > controllers > edit > angebot_controller

defmodule AndersWeb.Edit.AngebotController do
 use AndersWeb, :controller

 alias Anders.Projects
 alias Anders.Projects.Angebot

def create(conn, %{"angebot" => angebot_params}) do
  current_user = conn.assigns.current_user
  #changeset = Ecto.build_assoc(current_user, :projects, angebot_params)
  #changeset = Angebot.changeset(%Angebot{user_id: current_user.id}, post_params)

case Projects.create_angebot(angebot_params) do
  {:ok, angebot} ->
    conn
    |> put_flash(:info, "Angebot created successfully.")
    |> redirect(to: edit_angebot_path(conn, :show, angebot))
  {:error, %Ecto.Changeset{} = changeset} ->
    render(conn, "new.html", changeset: changeset)
 end
....
end

I can’t find the correct way to insert the current_user.id
Thanks for supporting my beginning..

Most Liked

dokuzbir

dokuzbir

Today we discussed in forum about user_id in changesets. This is a better approach than allowing cast :user_id in changeset
you can look post about user_id

So for your case

def create(conn, %{"angebot" => angebot_params}) do
      current_user = conn.assigns.current_user
     

    case Projects.create_angebot(angebot_params, current_user) do
      {:ok, angebot} ->
        conn
        |> put_flash(:info, "Angebot created successfully.")
        |> redirect(to: edit_angebot_path(conn, :show, angebot))
      {:error, %Ecto.Changeset{} = changeset} ->
        render(conn, "new.html", changeset: changeset)
     end
    ....
    end

    #lip/projects/projects.ex

    def create_angebot(attrs, %User{id: user_id}) do
      %Angebot{user_id: user_id}
      |> Angebot.changeset(attrs)
      |> Repo.insert()
    end

#lip/projects/angebot.ex
#and delete cast :user_id in changeset function
|> cast(attrs, [:title, :subtitle, :content, :online, :since, :zip, :city, :country])
NobbZ

NobbZ

Assuming angebot_params is a map:

Projects.create_angebot(Map.put(angebot_params, :user_id, current_user.id)) do

Assuming it’s a list:

Projects.create_angebot([{:user_id, current_user.id} | angebot_params])

Of course your create_angebot needs to know how to deal with the additional field.

wernerlaude

wernerlaude

sorry my fault.. now it seems to work.. Will check that.. Thanks so far..

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
shahryarjb
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
chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement