Function Repo.all/1 is undefined (module Repo is not available)

schema names should be singular because an ecto schema creates a struct, so it will generate things that are “objects” and you will want to pass them around like they are single things. Context names should be plural, because they refer to the collections in the tables.

1 Like

but in the github project ALL of the Schema names are plural like rooms, followers etc… that’s what I find confusing especially when I’m trying to follow the Ecto guides…

and I still have not gotten the CONTEXT thing, there is no context definition code that I can see, it seems to be file naming and file placement but I don see anything called CONTEXT instruction set anywhere I’ve read…

and I just finished reading the article that @kokolegorille sent me a link to, and even in that article the author has a problem defining what context is so the author skips any clear example and dives in into his application description of the different modules making up his application without discussing what the context is… either I’m missing something or I am reading the article with an accent… LoL.I tent to do that quite often… :slight_smile:

are you sure? It’s not a schema unless it says use Ecto.Schema, I think what you are looking at is the Context.

oh don’t look at the content of the existing project. That’s ben. He doesn’t know many elixir idioms yet. I’m helping him refactor it.

here’s the content of the rooms.ex under the schema folder:

defmodule Beef.Room do
  use Ecto.Schema
  import Ecto.Changeset

  @derive {Poison.Encoder,
           only: [
             :id,
             :name,
             :numPeopleInside,
             :isPrivate,
             :creatorId,
             :peoplePreviewList,
             :voiceServerId
           ]}
  @primary_key {:id, :binary_id, []}
  schema "rooms" do
    field(:name, :string)
    field(:numPeopleInside, :integer)
    field(:isPrivate, :boolean)
    field(:voiceServerId, :string)

    belongs_to(:user, Beef.User, foreign_key: :creatorId, type: :binary_id)
    embeds_many(:peoplePreviewList, Beef.UserPreview)

    timestamps()
  end

  @spec insert_changeset(
          {map, map} | %{:__struct__ => atom | %{__changeset__: map}, optional(atom) => any},
          :invalid | %{optional(:__struct__) => none, optional(atom | binary) => any}
        ) :: Ecto.Changeset.t()
  @doc false
  def insert_changeset(room, attrs) do
    room
    |> cast(attrs, [:id, :name, :creatorId, :isPrivate, :numPeopleInside, :voiceServerId])
    |> validate_required([:name, :creatorId])
    |> validate_length(:name, min: 2)
    |> unique_constraint(:creatorId)
  end
end

schema “rooms” do line in the rooms.ex file

it’s not been refactored yet. The architecture.md file describes the alternative organization plan.

rooms is the name of the table, which is plural. But Room (and room.ex) as the name of the schema or file is singular.

A context is usually not a one to one relation with schema… Rooms for Room, Blogs for Blog, but Accounts for User. I like to start with a Core context, and then split it when growing too much. You might have an Accounts context, for User and Role schemas.

You will find the API to manage schemas in the context… like list_blogs, get_blog, create_blog, update_blog, delete_blog etc.

mix phx.gen.context does not generate html, for this You need to use mix phx.gen.html, or mix phx.gen.json if You deal with an API. But it does generate context, migration and schema files. The later also build controller, templates and view.

While generators assume some conventions, You are still free to organize as You prefer. You might find different structures, like the one proposed by @ityonemo, like Poncho app, like moving changeset to context etc.

But as You are still learning, it might be better to follow convention, and maybe later change organization.

BTW Repo should not leak into controllers. Controllers should use API defined in contexts.

@kokolegorille
thanks this is great stuff… taking copious notes.
you are correct about the naming conventions which I’m trying to get handle on.

so far I’m ok with the commands:
mix phx.gen.html, mix phx.gen.json. and migration command

getting to the other ones soon… :slight_smile:

Thanks again.

politeness should be your best friend :blush:
I’m here from google! with same problem…
btw ty.