How to implement setters on Ecto (or Elixir structs) models

I come from the Rails mind and use to add a lot of customizations to the ActiveRecord model for string / line strings (like email addresses) or normalize phone numbers to use "-" as delimiters. I like the setter method because it saves the code that changes the DRY entry. Is this possible with Elixir / Ecto?

+3


source to share


1 answer


There is currently no mechanism. The best you can do is create a function like:

User.normalize(%User{}, conn.params["user"])

      



We are currently planning to move this code to custom types. So, for example, you implement an email type that knows how to properly execute and normalize data, and casting will work on Ecto queries and when assigning field values.

+4


source







All Articles