Home > Software design >  Using existing entries in an Ecto changeset function
Using existing entries in an Ecto changeset function

Time:01-09

I have a business_hours table (using MySQL) which contains the following fields:

location_id (foreign key)
starting_time (float [e.g. 17 = 5pm, 17.5 = 5:30pm])
ending_time (float)
day (integer [0 = monday, 6 = sunday])

My question is how would I go about creating an Ecto changeset function to validate that a new business_hours entry does not overlap any existing entry? I know how I would check for this in general (see below), but I am unsure on how to integrate this into a changeset function to validate this serverside.

(a.starting_time > b.ending_time or a.ending_time < b.starting_time) and a.day == b.day and a.location_id == b.location_id

CodePudding user response:

Remember that a changeset is a struct like any other, and the built-in validation functions accept an

  • Related