MatijaL

MatijaL

Understanding changesets... is it better to have one or multiple changesets?

Hi,
this is probably a silly question, but I’m an Elixir and Phoenix beginner, learning new things every day.

Let’s say I have a project schema with couple of fields like title, description and status.

schema "projects" do
    field :title, :string
    field :description, :string
    field :status, :string
end

I want to be able to update title and description from one function and I want to have completely different function to update just status. Would it be better to use one changeset for both of those functions or would it be better to split it into two changeset?

def changeset(%Project{} = project, attrs) do
    project
    |> cast(attrs, [:title, :description, :status])
end

vs

def base_changeset(%Project{} = project, attrs) do
    project
    |> cast(attrs, [:title, :description])
end

def status_changeset(%Project{} = project, attrs) do
    project
    |> cast(attrs, [:status])
end

I hope someone will be able to help me understand this.

First 2 of 2 Posts! Switch mode

APB9785

APB9785

Creator of ECSx

You will probably want two changesets. Especially if you are using Phoenix, because when you construct the forms, it will use the changeset for validation. So if you have a form with inputs for title and description, and then another form with a dropdown menu to change the status, you want the appropriate changeset for each use case.

sbuttgereit

sbuttgereit

This is one of those things where there is probably more than one way valid way to proceed so long as you’re consistent within the project that you’re working. I’ll give you my take on it. Also, I’m assuming that the changeset will ultimately be used to update a database somewhere. This is not the only use case for changesets, but its the one I’m most familiar with and for which I’ve formed some opinions so will be my focus here.

I use the changeset mostly for whole record validation. Does the data I’m about to persist meet the criteria for a record to be valid or not, and if not how does it fail? I do use specialized functions like you are thinking about for isolated data changes like only performing status changes. These specialized functions are effectively a layer above the changeset and are closer to an internal API; they will ultimately call a generalized changeset function for validation, after they’ve performed any special operation or validation that pertains to the specialized operation (i.e. the status change).

There are a couple reasons for this. First, I want a single place to express what constitutes a valid data record (that’s a lie, I do a lot of in database constraint management as well which also acts as data validation, but view that as last resort at the point of information truth.) If I start specializing the changesets to for a given data operation I lose some of that simplicity of having an in application definition for validation; the idea of ‘valid’ for a single record starts to get spread around between functions and I expect would become more difficult to manage or track down problems. Next, it gives me that extra level of abstraction that allows me to separate some of the persistence related concerns from the business logic concerns; by expecting to manage things like statuses and data in the API, how it’s persisted becomes less important to the business logic and to me the changeset feels much closer to that persistence layer.

I do have multiple changeset functions for records, but I vary based on coarse grained data operation: insert vs. update. Aside from pure validation, I will allow some defaulting on insert operations whereas update operations assume the record is whole with only changes to otherwise valid data being made. But that’s pretty limited with about 75% of my changeset use being about just validation of passed data.

Anyway, that’s my perspective.

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement