wernerlaude

wernerlaude

How to get all entries by user_id

In AR this is so simple

@articles = current_user.articles

How to do in Ecto?

def index(conn, _params) do
  current_user = conn.assigns.current_user
  meetings = Listings.list_meetings() #works..sure but I only want current_user ones..

Tried all what I could find in goo, but no success!

#meetings = Repo.get_by!(Meeting, user_id: current_user.id) #nope, but anyway …is this getting all or only one entry?
=> Enumerable not implemented for

meetings = Repo.all(Meeting) |> where(user_id: current_user.id)
=> MeetingController.init/1 is undefined.

some help would be great…thanks

Most Liked

peerreynders

peerreynders

You can if you want to:

field :user_id, :id, primary_key: true

and on legacy databases you sometimes have to.

Ecto.Schema.field/3
Ecto.Migration.table/2
Ecto.Migration.add/3

It still won’t teach you SQL - you’re still on your own for that …

We’re also going assume that you’re comfortable working with relational databases and SQL. You don’t need to be an expert, but you should be familiar with tables, columns, indexes and how to write queries. There are many online tutorials that can walk you through the basics.

narand

narand

The field user_id (i.e. the field for the foreign key) will be automatically added when using belongs_to, so you should not add it yourself.

Should you need to customize the foreign key field name (say when you have two associations to the same schema), you can pass the foreign_key option to belongs_to (see belongs_to/3).

peerreynders

peerreynders

Sure - however AR isn’t available in Ecto (or Phoenix) and I don’t know how much effort it would be to set up a piggybacked environment just for the purpose of generating SQL from active record.

Joe Celko’s stuff is interesting but from what I remember some of the content dealt with a “(prerelease-)reference-level ANSI SQL” that may or may not work on most platforms in the manner presented (so sometimes “translation” may be required). While SQL isn’t exactly moving at the same pace as JavaScript, a lot of his content has been around for awhile, the last (advanced) book update being from 2014.

For the purposes of getting the most out of Programming Ecto
http://www.postgresqltutorial.com/
is probably the best bet. The bits of the tutorial I have come across seem to do the job.

Where Next?

Popular in Questions Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
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
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Codball
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

Other popular topics Top

minhajuddin
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
yurko
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
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
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
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
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement