> ## Documentation Index
> Fetch the complete documentation index at: https://forest-chore-open-api.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Multiple-records relationships

Relationships that point to multiple records are displayed in the frontend in the "Related Data" and "Explorer" tabs.

## One-to-Many relationships

In a one-to-many relationship, one record from a Collection is attached to multiple records of another Collection.

Think about countries and towns: a country has multiple towns, and each town belongs to a country.

<CodeGroup>
  ```javascript Node.js / Cloud theme={null}
  // Link 'countries' to 'towns'
  agent.customizeCollection('countries', collection => {
    collection.addOneToManyRelation('myTowns', 'towns', {
      originKey: 'country_id',
      originKeyTarget: 'id', // Optional (uses primary key of countries by default)
    });
  });
  ```

  ```ruby Ruby theme={null}
  ForestAdmin.customize do
    # Link 'countries' to 'towns'
    customize_collection('countries') do |collection|
      collection.add_one_to_many_relation('myTowns', 'towns',
        origin_key: 'country_id',
        origin_key_target: 'id' # Optional (uses primary key of countries by default)
      )
    end
  end
  ```
</CodeGroup>

## Many-to-Many relationships

In a many-to-many relationship, 3 Collections are used instead of 2 to build the relationship.

This allows multiple records from one Collection to be attached to multiple records from another Collection.

For instance, on a movie recommendation website, each user can rate many movies, and each movie can be rated by many users. The 3 Collections used are `users` (the "origin"), `ratings` (the "through"), and `movies` (the "foreign" Collection).

<CodeGroup>
  ```javascript Node.js / Cloud theme={null}
  // Create one side of the relationship ...
  agent.customizeCollection('users', collection => {
    collection.addManyToManyRelation('ratedMovies', 'movies', 'ratings', {
      originKey: 'user_id',
      foreignKey: 'movie_id',
    });
  });

  // ... and the other one
  agent.customizeCollection('movies', collection => {
    collection.addManyToManyRelation('whoRatedThisMovie', 'users', 'ratings', {
      originKeyTarget: 'id', // Optional (uses primary key of movies by default)
      originKey: 'movie_id',
      foreignKey: 'user_id',
      foreignKeyTarget: 'id', // Optional (uses primary key of users by default)
    });
  });
  ```

  ```ruby Ruby theme={null}
  ForestAdmin.customize do
    # Create one side of the relationship ...
    customize_collection('users') do |collection|
      collection.add_many_to_many_relation('ratedMovies', 'movies', 'ratings',
        origin_key: 'user_id',
        foreign_key: 'movie_id'
      )
    end

    # ... and the other one
    customize_collection('movies') do |collection|
      collection.add_many_to_many_relation('whoRatedThisMovie', 'users', 'ratings',
        origin_key_target: 'id', # Optional (uses primary key of movies by default)
        origin_key: 'movie_id',
        foreign_key: 'user_id',
        foreign_key_target: 'id' # Optional (uses primary key of users by default)
      )
    end
  end
  ```
</CodeGroup>

## External relationships

External relationships allow defining Collections which will only be available through the "Related Data" section of a given model.

<Warning>
  External relationships do not support pagination.
</Warning>

<CodeGroup>
  ```javascript Node.js / Cloud theme={null}
  const states = [
    { code: 'AK', name: 'Alaska', zip: [99501, 99950], closeTo: [] },
    { code: 'AL', name: 'Alabama', zip: [35004, 36925], closeTo: ['TE', 'MI', 'GE'] },
    { code: 'AR', name: 'Arkansas', zip: [71601, 72959], closeTo: ['OK', 'TX', 'LO'] },
    { code: 'AZ', name: 'Arizona', zip: [85001, 86556], closeTo: ['NM', 'CO', 'NE'] },
    { code: 'CA', name: 'California', zip: [90001, 96162], closeTo: ['OR', 'NE'] },
    // ....
  ];

  agent.customizeCollection('address', collection => {
    collection.addExternalRelation('nearStates', {
      // Define schema of the records in the relationship.
      schema: { code: 'Number', name: 'String' },

      // Which fields are needed from the parent record to run the handler?
      // Dependencies are optional: by default only the primary key of address would be
      // provided.
      dependencies: ['country', 'zipCode'],

      // Compute list of records from the parent record
      listRecords: async ({ country, zipCode }) => {
        if (country === 'USA') {
          const state = states.find(s => s.zip[0] < zipCode && zipCode < s.zip[1]);
          return states.filter(s => state.closeTo.includes(s.code));
        }

        return [];
      },
    });
  });
  ```

  ```ruby Ruby theme={null}
  STATES = [
    { 'code' => 'AK', 'name' => 'Alaska', 'zip' => [99501, 99950], 'closeTo' => [] },
    { 'code' => 'AL', 'name' => 'Alabama', 'zip' => [35004, 36925], 'closeTo' => ['TE', 'MI', 'GE'] },
    { 'code' => 'AR', 'name' => 'Arkansas', 'zip' => [71601, 72959], 'closeTo' => ['OK', 'TX', 'LO'] },
    { 'code' => 'AZ', 'name' => 'Arizona', 'zip' => [85001, 86556], 'closeTo' => ['NM', 'CO', 'NE'] },
    { 'code' => 'CA', 'name' => 'California', 'zip' => [90001, 96162], 'closeTo' => ['OR', 'NE'] },
    # ....
  ]

  ForestAdmin.customize do
    customize_collection('address') do |collection|
      collection.add_external_relation('nearStates',
        # Define schema of the records in the relationship.
        schema: { 'code' => 'Number', 'name' => 'String' },

        # Which fields are needed from the parent record to run the handler?
        # Dependencies are optional: by default only the primary key of address would be provided.
        dependencies: ['country', 'zipCode'],

        # Compute list of records from the parent record
        list_records: ->(record) {
          if record['country'] == 'USA'
            state = STATES.find { |s| s['zip'][0] < record['zipCode'].to_i && record['zipCode'].to_i < s['zip'][1] }
            STATES.select { |s| state['closeTo'].include?(s['code']) }
          else
            []
          end
        }
      )
    end
  end
  ```
</CodeGroup>
