Track Magic has special support for Has Many/Belongs To associations in your Rails applications. You can create a new subrecord belonging to the current record, select all subrecords that belong to the current record (for example, select all employees belonging to the current office) or select the owner of a record (for example, find the office an employee works at). Note: These features will work whether you use Track Magic to set up the associations (see Creating Associations) or whether they have been set up manually.
Creating a New Subrecord Belonging to the Current Record
To create a new record that belongs to the current record, use the New submenu in the Rails menu.
This menu lists all of the tables that have records belonging to the current table. Select the table you want to add the new record to. For example, to add a new employee to an office, choose New > Employee, as shown above. A new record is added to the employee table. The office_id field is automatically filled in with the record ID of the currently selected office.
The final step is to update the actual Rails table by choosing Update All Changes to Rails from the Rails menu (see Uploading Data).
Selecting Records that Belong To the Current Record
To find out what records in another table belong to the current record, use the Records Belonging To This Record submenu. This submenu is itself a submenu of the Show Subset of Local Data submenu of the Rails menu.
The submenu lists all of the tables that have records belonging to the current table. Choosing a table brings that table forward and selects all of the records belonging to the current record in the first table. For example, you could use this to display all of the employees that work at a particular office.
Finding the Record the Current Record Belongs To
If the current record belongs to a record in another table, you can find the owner record using the Record this Record Belongs to submenu. This submenu is itself a submenu of the Show Subset of Local Data submenu of the Rails menu.
The submenu lists all of the tables that own records in the current table. Choosing a table from the menu brings that table forward and locates the record that owns the current record in the original table. For example you could use this to find out what office a particular employee works at.
It's possible for a table to both have records that belong to it and for records in the table to belong to other tables. In that case both the Records Belonging To This Record and the Record this Record Belongs to submenus will appear in the Show Subset of Local Data submenu of the Rails menu.
Using Associations in a Panorama Formula
Track Magic adds two special custom functions to Panorama that allow associations to be used in a Panorama formula. This makes it easy to use associations in a Panorama report. You can also use associations as scratch fields (see Field Configuration).
The RailsHasMany( Function
This function gets data from the "child" records of the current database (as defined by "hasmany" in the Rails model). There are three parameters: table, formula and separator:
railshasmany(table,formula,separator)
The table parameter specifies the table that contains the child records. Normally you'll need to enclose the table name in quotes. The formula parameter is the TEXT of the Panorama formula to be used to gather data from the child database. This must be enclosed in some kind of quotes (Panorama allows you to use single quotes, double quotes, smart quotes, curly braces or pipes to quote text). Any fields included in this formula must be fields in the child database. The final parameter is the separator character to be used between multiple records returned by this function.
To illustrate this function we'll add a scratch field to an offices table that lists the employees working at each office. Start by opening the Local Field Configuration… dialog in the Rails menu, then press the Advanced button to expand the dialog to show scratch fields.
Press the + icon in the upper right hand corner to add a new scrach field. Then type in this Panorama formula using the railshasmany( function.
When you close the dialogs the new scratch field appears. Each cell in this field displays the number of the employees associated with that office:
In the previous example the formula parameter contained a single Panorama field, but you can combine multiple fields in the formula.
WIth this formula each office record now displays both the employee names and their phone numbers.
To learn more about writing Panorama formulas see Chapter 23 of the Panorama Handbook.
The RailsBelongsTo( Function
This function gets data from the "parent" record of the current database (as defined by "belongsto" in the Rails model). There are two parameters: model and formula:
railsbelongsto(model,formula)
The model parameter specifies the class that contains the parent records (singular form). Normally you'll need to enclose the table name in quotes. The formula parameter is the TEXT of the Panorama formula to be used to gather data from the parent database. This must be enclosed in some kind of quotes. Any fields included in this formula must be fields in the parent database.
To illustrate this function we'll add a scratch field to an employee table that displays the city and state of the office the employee works at. Start by opening the Local Field Configuration… dialog in the Rails menu, then press the Advanced button to expand the dialog to show scratch fields. Press the + icon in the upper right hand corner to add a new scrach field. Then type in this Panorama formula using the railsbelongsto( function.
When you close the dialogs the new scratch field appears. Each cell in this field displays the city and state for that employees office: