# Smart Relationships

**URL:** <https://community.forestadmin.com/t/smart-relationships/134>\
**Category:** Help me!\
**Created:** [May 11, 2020, 3:41pm UTC](https://community.forestadmin.com/t/smart-relationships/134 "2020-05-11T15:41:22Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![anon39848301](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@anon39848301](https://community.forestadmin.com/u/anon39848301)\
**Post date:** [May 12, 2020, 8:47am UTC](https://community.forestadmin.com/t/smart-relationships/134/2 "2020-05-12T08:47:21Z")

</div>

Hello @Jonathan_Nye !

In order to make this work, you would have to implement routes related to this new relationship (That’s why smart relationships are set to read only by default).

Here is a quick example using `Cars` and `Users` models. Users have a field `car` that should match with the Cars `name` field.

In case I want to have a smart relationship `car_relation`, I would need to add :  
In `forest/users.js`:

```javascript
collection('users', {
  actions: [],
  fields: [{
    field: 'car_relation',
    type: 'String',
    reference: 'cars.name',
    get: function (user) {
      return models.cars.findOne({ where: { name: user.car }});
    }
  }],
  segments: [],
});

```

Then, if you want to be able to create new records with this relation, you need to override the POST route in `routes/users.js` :

```javascript
// Create a User with the new relation
router.post('/users', permissionMiddlewareCreator.create(), async (request, response, next) => {
  try {
    const requestBody = request.body;
    // if the relation is present
    if(requestBody.data.relationships.car_relation.data.id) {
      // Find the associated relation
      const relationshipCar = await models.cars.findByPk(request.body.data.relationships.car_relation.data.id);
      // Manually update the given body to handle the relation
      requestBody.data.attributes.car = relationshipCar.name;
    }
    // Update the record and send it back
    const recordCreator = new RecordCreator(models.users);
    const recordToCreate = await recordCreator.deserialize(requestBody);
    const newRecord = await recordCreator.create(recordToCreate);
    const serializedRecord = await recordCreator.serialize(newRecord);
    response.send(serializedRecord);
  } catch (err) {
    next(err);
  }
});

```

Finally, if you want to update the record via the same mecanism, you will need to add a relationship routes, in the same `routes/users.js` file :

```javascript
router.put('/users/:userId/relationships/car_relation', async (request, response, next) => {
  try {
    // data.id contains the id of the selected car in the UI
    if(request.body.data.id) {
      // Get all the record needed and update
      const user = await models.users.findByPk(request.params.userId);
      const car = await models.cars.findByPk(request.body.data.id);
      user.car = car.name;
      await user.save();
      response.send(user);
    }
  } catch (err) {
    next(err);
  }
});

```

Hope this will help 🙂

---

_[View the full topic](https://community.forestadmin.com/t/smart-relationships/134)._
