Imagine a simple I-O-U app where there are a User model and a Transaction model.
The User table has the following columns:
- id
- name
The Transaction table has the following columns:
- lender_id (foreign key to
User.id) - borrower_id (foreign key to
User.id) - date
A user can only lend or borrow to another user that exists in the User table.
An example would be:
User:
id | name
-------------
1 | name A
2 | name B
3 | name C
Transaction:
lender_id | borrower_id | date
----------------------------------
1 | 2 | date1
1 | 3 | date2
3 | 2 | date3
How would I implement the associations in the model classes?
On transaction:
On user:
This gives you:
And
Hope this helps!