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

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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
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
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

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 th...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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

We're in Beta

About us Mission Statement