Naming conventions for Through models

I find myself creating through schema’s like this:

defmodule: Author do
    has_many(:post_authors, PostAuthor, foreign_key: author_id)
    has_many(:posts, Post, through: [:post_authors, :post])
    ...
end

defmodule: PostAuthor do  # What is the naming convention for this Schema?
    belongs_to(:post, Post)
    belongs_to(:author, Author)
end

defmodule: Post do
    has_many(:post_authors, PostAuthor, foreign_key: post_id)
    has_many(:authors, Author, through: [:post_authors, :author])
    ...
end

Is there a good convention to follow for naming the through-model? Do you call it PostAuthor, or PostAuthorThrough and why?

PostAuthorThrough taking out of Ecto context doesn’t mean much, right? PostAuthor seems a good naming. I think you need to create the vocabulary based on your domain, and not on technical details (hence not using -through).

Personally, sometimes I even make up a new noun if that is more meaningful. For example, for many-to-many entries joining an Appointment and a Contact: I could name it AppointmentContact, but instead I name it Attendee.

3 Likes

For User and Group I like to use membership…

Maybe authorship? :slight_smile:

2 Likes

Thanks for the answer!

I digged around a bit on StackOverflow, and accepted this answer as the conclusion.

  1. If there is a meaningful name from the domain, use that.
  2. else, concatenate the two names.