I hope I am describing it properly. In django web framework one can do
GENDER_CHOICES = (
('m', 'Male'),
('f', 'Female'),
('c', 'Couple'),
)
class UserProfile(models.Model):
# ... some fields
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
In the above code it is defined a tuple of possible choices and the model field can only take one of the values “m”, “f” or “c”. The important thing is that GENDER_CHOICES does not represent a table in the database.
Is there an established pattern to do the same thing with an Ecto field?
Well, I can think of couple of ways. First of all, this sort of thing can be handled on the database level. If using PostgreSQL, it has an “Enum” types PostgreSQL: Documentation: 9.2: Enumerated Types.
but I do not know how complete is that or is that usable at all. You’ll need to investigate.
The other option is to simply use the String/Varchar/Text type and enforce the values using Ecto’s validations mechanism, to allow only certain values: