# Computed foreign keys

{% hint style="success" %}
This is the official documentation of the `agent_ruby` Ruby agent.
{% endhint %}

You may want to create a relationship between 2 Collections, but you don't have a foreign key that is ready to use to connect them.

This is done with the following steps:

* create a new Field containing a foreign key,
* make the Field filterable for the `In` operator (see [Under the hood](/developer-guide-agents-ruby/agent-customization/relationships/under-the-hood.md) as to why this is required),
* create a relation using it.

### Displaying a link to the last message sent by a customer

We have 2 Collections: `Customers` and `Messages`, linked together by a `one-to-many` relationship.

We want to create a `ManyToOne` relationship with the last message sent by a given customer.

```ruby
include ForestAdmin::Types

@create_agent.customize_collection('customer') do |collection|
  # Create foreign key
  collection.add_field(
    'lastMessageId',
    ComputedDefinition.new(
      column_type: 'Number',
      dependencies: ['id'],
      values: proc { |customers, context|
        customer_ids = customers.map { |r| r['id'] }

        # We're using Forest Admin's Query Interface
        filter = Filter.new(
          condition_tree: ConditionTreeLeaf.new('customer_id', Operators::IN, customer_ids)
        )
        aggregation = Aggregation.new(operation: 'Max', field: 'id', groups: [{ field: 'customer_id' }])
        rows = context.datasource.get_collection('message').aggregate(filter, aggregation)

        customers.map do |customer|
          row = rows.find { |r| r['group']['customer_id'] == customer['id'] }
          row ? row['value'] : nil
        end
      }
    )
  )
  .replace_field_operator('lastMessageId', Operators::IN) do |last_message_ids, context|
    filter = Filter.new(
      condition_tree: ConditionTreeLeaf.new('id', Operators::IN, last_message_ids)
    )
    records = context.datasource.get_collection('message').list(filter, Projection.new(['customer_id']))

    { field: 'id', operator: 'In', value: records.map { |r| r['customer_id'] } }
  end
  .add_many_to_one_relation('lastMessage', 'message', { foreign_key: 'lastMessageId' })
end
```

### Connecting collections without having a shared identifier

You have 2 Collections and both contain users: one comes from your database, and the other one is connected to the CRM that your company uses.

There is no common id between them that can be used to tell Forest Admin how to link them together, however, both Collections have `firstName`, `lastName`, and `birthDate` fields, which taken together, are unique enough.

```ruby
include ForestAdmin::Types

# Concatenate firstname, lastname and birthData to make a unique identifier
# and ensure that the new field is filterable
def create_filterable_identity_field(collection)
  # Create foreign key on the collection from the database
  collection.add_field(
    'userIdentifier',
    ComputedDefinition.new(
      column_type: 'String',
      dependencies: %w[firstName lastName birthDate],
      values: proc { |users| users.map { |u| "#{u['firstName']}/#{u['lastName']}/#{u['birthDate']}" } }
    )
  )

  # Implement 'In' filtering operator (required)
  collection.replace_field_operator('userIdentifier', Operators::IN) do |values, _context|
    {
      aggregator: 'Or',
      conditions: values.map do |value|
        {
          aggregator: 'And',
          conditions: [
            { field: 'firstName', operator: Operators::EQUAL, value: value.split('/')[0] },
            { field: 'lastName', operator: Operators::EQUAL, value: value.split('/')[1] },
            { field: 'birthDate', operator: Operators::EQUAL, value: value.split('/')[2] }
          ]
        }
      end
    }
  end
end

# Create relationship between databaseUsers and crmUsers
def create_relationship(collection)
  collection.add_one_to_one_relation(
    'userFromCrm',
    'crm_users',
    {
      origin_key: 'userIdentifier',
      origin_key_target: 'userIdentifier'
    }
  )
end

# Create relationship between crmUsers and databaseUsers
def create_inverse_relationship(collection)
  collection.add_many_to_one_relation(
    'userFromDatabase',
    'database_users',
    {
      foreign_key: 'userIdentifier',
      foreign_key_target: 'userIdentifier'
    }
  )
end

@create_agent.customize_collection('database_users', &method(:create_filterable_identity_field))
@create_agent.customize_collection('crm_users', &method(:create_filterable_identity_field))
@create_agent.customize_collection('database_users', &method(:create_relationship))
@create_agent.customize_collection('crm_users', &method(:create_inverse_relationship))
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.forestadmin.com/developer-guide-agents-ruby/agent-customization/relationships/computed-fks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
