I faced with next question when validate related record. Imagine I have an event with timings and an attend with time specified by a user. My task is to validate that time is in timings of the event. So I have at least 3 options:
def create_attend(event, user, attrs) do
event = event |> preload_somwehre_timings()
attend = %Attend{user_id: user.id, event_id: event.id, event: event}
with ...
Attend.changeset(attend, attrs)
....
end
# Somewhere in Attend module
def validate_time_is_in_bounds(changeset) do ... end
-
Use almost the same approach but extract validations, adjust them for
Multi
and use. But in my case it is not required to use a transaction. -
Use extra layer module called
AttendRequest
and perform all the validations inside this module and then just to convert it beforeRepo.insert
to regular attend struct. Does it make sense? Why I would like this option is that after create I don’t have preloaded fields.