Integrate recursion into a changeset

Hello guys
How Can I integrate this

into a changeset.

What do you mean? A changesrt is a way to write data to validate data changes, generally for writing to the database. The linked question seems unrelated.

Can you explain what problem you’re trying to solve, or what you’re trying to accomplish?

Sorry I’ll try to explain better.

I trying to translate in elixir/ecto my ruby/ar code:

def check_for_duplicates
     while List.pending.where("amount = ?", self.amount).any? do
        self.amount += 0.01

So if in my List([1.01, 1.02, 1.04]) there is identical amount (ex 1.01) I have to increment the amount up to 1.03 before save. I was thinking that I could accomplish it within a changeset with recursion. Sorry for my poor English and thanks for your time

What list are you talking about? What is List? What is this pending and pending.where on it? What does the do/end block do in it?

I speak Ruby, so I get what you are doing there (in a list of related models, if any have a matching amount to me, then increment my amount and check again until my amount is unique). But that seems like a really odd band-aid solution to a deeper problem that probably should be solved by a uniqueness constraint in your database and/or a sequence generator. The algorithm you have there is vulnerable to race conditions.

Even within Ruby, I’d implement this logic with an each_cons so you can find a gap in your numbering scheme without having to re-query in a while loop. Within Elixir Enum.chunk can work like each_cons so the algorithm would translate better. Note, this approach still has a database race condition. If you can push this constraint into the database, you’ll be better off overall.