0

I am currently making an app for punching in and out employees.

I have three tables, one for the Employes, one for the PunchIn and one for the PunchOut.

Diagram

What I want is the following : when I create a PunchIn, it change the is_in entry (of the table Employees) to true and when I create a PunchOut, the is_in is false.

How can I deal with relations to make this?

Thanks!

Félix Desjardins
  • 3,223
  • 3
  • 21
  • 36

2 Answers2

0

Relations are not the best use case here. You need your employee entity to create a Punchin entity. So, why not directly update your employee entity instead of using relations ?

An example:

var emp = ds.Employees.find("ID = 1");
var myPunchin = new ds.Punchin({
    employee: emp
});
myPunchin.save();

myPunchin.employee.is_in = true;
myPunchin.employee.save();

Even better without relations, you can replace myPunchin.employee by emp.

Yann
  • 478
  • 5
  • 10
0

Assuming the employee entity already exists, it's just:

punchInEntity.Employee.is_in = true;

and

punchOutEntity.Employee.is_in = false;

Be sure to save both entities afterward:

punchOutEntity.save();
punchOutEntity.Employee.save();

However, you might want to consider making is_in a computed attribute. In it's method, you could search for a punchOutCollection entity that is after the current date/time. Return false if one is found and true if not (as long as there is at least one punchInCollection entity). That way, you don't have to worry about updating is_in and it will always be correct.

Jeff G
  • 1,996
  • 1
  • 13
  • 22
  • I forgot to mention that you may want to change the Employee attribute names to employee to match the convention of beginning attribute names with lowercase. While we're at it, why not rename punchInCollection and punchOutCollection to punchIns and punchOuts? – Jeff G Oct 03 '16 at 13:42