mcpbeat Sign in

Ecto Patterns Skill for Claude

Ecto patterns — schemas, changesets, queries, migrations, Multi, associations, preloads, upserts. Use when editing Repo calls, Ecto.Query, or schema fields. Skip for Ash.

7k tokens
context cost
the whole folder, loaded on every use
6
files
instructions only
0
copies elsewhere
how many repositories repackaged it
514
stars on the repo
on the repository, not the skill itself

Install

one command, takes just this skill from the repository
npx skills add https://github.com/oliver-kriska/claude-elixir-phoenix --skill ecto-patterns

What comes with it

24 135 bytes besides the instruction
references/changesets.md
references/fulltext-search.md
references/migrations.md
references/queries.md
references/transactions.md

The instruction itself

8 sections, as written by the author

Ecto Patterns Reference

Reference for working with Ecto schemas, queries, and migrations.

Iron Laws — Never Violate These

  • CHANGESETS ARE FOR EXTERNAL DATA — Use cast/4 for user/API input, change/2 or put_change/3 for internal trusted data
  • NEVER USE :float FOR MONEY — Always use :decimal or :integer (cents)
  • NO RAILS-STYLE POLYMORPHIC ASSOCIATIONS — They break foreign key constraints; use multiple nullable FKs or separate join tables
  • ALWAYS PIN VALUES IN QUERIESu.name == ^user_input is safe, string interpolation causes SQL injection
  • PRELOAD COLLECTIONS, NOT INDIVIDUALS — Preloading in loops = N+1 queries
  • CONSTRAINTS BEAT VALIDATIONS FOR RACE CONDITIONS — Validations provide quick feedback, constraints provide DB-level safety
  • SEPARATE QUERIES FOR has_many, JOIN FOR belongs_to — Avoids row multiplication
  • NO IMPLICIT CROSS JOINSfrom(a in A, b in B) without on: creates Cartesian product
  • DEDUP BEFORE cast_assoc WITH SHARED DATA — When multiple parents share child data, deduplicate child records BEFORE building changesets. Dedup only works within a single changeset

Quick Schema Template

defmodule MyApp.Context.Entity do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id

  schema "entities" do
    field :name, :string
    field :status, Ecto.Enum, values: [:draft, :active, :archived]
    field :amount_cents, :integer  # Never :float for money!
    belongs_to :user, MyApp.Accounts.User
    timestamps(type: :utc_datetime_usec)
  end

  def changeset(entity, attrs) do
    entity
    |> cast(attrs, [:name, :status, :amount_cents])
    |> validate_required([:name])
    |> foreign_key_constraint(:user_id)
  end
end

Quick Decisions

cast vs put_change vs change

| Function | Use When |

|----------|----------|

| cast/4 | External data (user input, API) |

| put_change/3 | Internal trusted data (timestamps, computed) |

| change/2 | Internal data from existing struct |

Preload Strategy

| Relationship | Strategy |

|--------------|----------|

| belongs_to | JOIN (single query) |

| has_many | Separate queries (avoid row multiplication) |

Common Anti-patterns

| Wrong | Right |

|-------|-------|

| field :amount, :float | field :amount_cents, :integer |

| "SELECT * WHERE name = '#{name}'" | from(u in User, where: u.name == ^name) |

| Repo.all(User) \|> Enum.filter(& &1.active) | from(u in User, where: u.active) |

| Preloading in loops | Repo.preload(posts, :comments) |

| Repo.get!(User, user_id) with user input | Repo.get(User, id) + handle nil |

| {:ok, _} = Repo.update(cs) inside Repo.transaction | case/with + Repo.rollback(cs) (see transactions.md) |

References

For detailed patterns, see:

  • references/changesets.md - cast vs put_change, custom validations, prepare_changes
  • references/queries.md - Composable queries, dynamic, subqueries, preloading
  • references/migrations.md - Safe migrations, concurrent indexes, NOT NULL
  • references/transactions.md - Repo.transact, Ecto.Multi, upserts

Other skills for the same job

different authors, same section of the catalogue
Doc Coauthoring
by anthropics
vendor ×10

Guide users through a structured workflow for co-authoring documentation. Use when user wants to write documentation, proposals, technical specs, decision docs, or similar structured content. This workflow helps users efficiently transfer context, refine content through iteration, and verify the doc works for readers. Trigger when user mentions writing docs, creating proposals, drafting specs, or similar documentation tasks.

4k tokens
Changelog Generator
by frostant
×9

Automatically creates user-facing changelogs from git commits by analyzing commit history, categorizing changes, and transforming technical commits into clear, customer-friendly release notes. Turns hours of manual changelog writing into minutes of automated generation.

774 tokens
Test Driven Development
by w95
×7

Use when implementing any feature or bugfix, before writing implementation code

2k tokens
Writing Plans
by ZhanlinCui
×4

Use when you have a spec or requirements for a multi-step task, before touching code

816 tokens
Writing Skills
by ZhanlinCui
×4

Use when creating new skills, editing existing skills, or verifying skills work before deployment

26k tokens scripts
Crafting Effective Readmes
by softaworks
×3

Use when writing or improving README files. Not all READMEs are the same — provides templates and guidance matched to your audience and project type.

15k tokens
Humanizer
by softaworks
×3

| Remove signs of AI-generated writing from text. Use when editing or reviewing text to make it sound more natural and human-written. Based on Wikipedia's inflated symbolism, promotional language, superficial -ing analyses, vague attributions, em dash overuse, rule of three, AI vocabulary words, negative parallelisms, and excessive conjunctive phrases.

6k tokens
Opentrons Integration
by christophacham
×3

Official Opentrons Protocol API for OT-2 and Flex robots. Use when writing protocols specifically for Opentrons hardware with full access to Protocol API v2 features. Best for production Opentrons protocols, official API compatibility. For multi-vendor automation or broader equipment control use pylabrobot.

9k tokens scripts

How to use it

Copy the folder

Take oliver-kriska/claude-elixir-phoenix-ecto-patterns from the repository into ~/.claude/skills for personal use, or into .claude/skills inside a project.

Check the name does not clash

The agent identifies a skill by the name field in its header. Two skills with the same name cannot sit side by side — one of them will be ignored.