I have two models
- absence: amount, staff_id
- staff: id, leave_balance
inside the absence model I want to edit the staff member which has their id inside staff_id and then I want to change their leave_balance but I do not know how to access that attribute in the staff model according to the staff_id in the current model using my method in absence
First have a look at the extensive Rails Guide to Active Record Associations to understand the concept.
Then you can do this:
Staff.rb
Absence.rb
Now you can access the attribute by, let’s say,
Absence.first.staff.leave_balance -= 1or within an instance method in the Absence modelself.staff.leave_balance = ...