msyavuz

msyavuz

Ecto schema inside its own context?

Hello, i am a newbie in both phoenix and elixir and its been a blast so far. I have a question about ecto schemas and phoenix contexts.

Suppose i have two tables called clubs and club_members. While generating a new schema with phx.gen.schema

  1. Should i create the club schema under club context ?

  2. Or should i create club schema without context and put member schema under club context?

First approach results in module name being Club.Club but directory structure seems better. While module naming in second approach seems better but club schema sits outside club directory.

I guess what i am asking is which one of these is better?

/001   club/  member.ex
/002   club.ex
/001   club/  member.ex
/001   club/  club.ex

Marked As Solved

sodapopcan

sodapopcan

Generally if you are using contexts as a boundary, then you put all the schemas belonging to a context under it.
Also by convention, if there isn’t a good unique name for a context (which there often isn’t) then it’s the pluralized version of its primary schema. You could just call your context Club if you want, but this can become confusing and it also makes it impossible to alias the schema within the context as they will have the same name. So the “normal” thing to do is this:

lib/my_app/clubs/club.ex
lib/my_app/clubs/member.ex
lib/my_app/clubs.ex

This way the directory structure mimics the module structure. Some people like to put the context file in the context directory, which is fine, whatever floats your boat!

Technically, though, you are free to do whatever you want! The generators push us towards certain conventions, but otherwise Phoenix doesn’t impose any conventions on us.

Also Liked

zachallaun

zachallaun

The most common way to solve this would be to call the context clubs, so you would have Clubs.Club and Clubs.Member.

msyavuz

msyavuz

Thank you for the explanation!

Where Next?

Popular in Questions Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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

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
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement