This document describes best practices for getting objects via the link tables in TUSK.
Throughout TUSK objects are linked together via link tables. These link tables are used to create relationships between objects.
Although a number of objects have built in methods to easily get related objects, there are instances where code needs to be written. The process is fairly simple, get a link definition, call it's get_parents method with the primary key of the child and get the parent objects from that list.
Generic with Conditional
The above example returns all parents associated via the link table. In some instances a limited set of parents is wanted. We can accomplish that by passing in conditions.
The condition can be explicitly applied to the parent or link table by prepending "parent." or "link." to the field name.
List of User Courses
Relationships between courses and users are maintained in link_course_user. To get an array of course objects for a particular course we use a method in the user object:
This gives us an array of course objects which can be looped over etc.
List of User Courses with Conditions
The above example returns all courses associated with a user in link_course_user, but there are times when only a limited set of courses is wanted. On these occasions we can pass a condition to the parent_course method:
There are two tables involved in this action; course and link_course_user. In most instances throughout the system the condition field appears in only one of the tables, and doesn't need to be specified. For clarity, one can prepent "parent" or "link" to the conditional to specify which table that condition should be applied.