# ActiveMDX Model Relationships

Models can relate to other models in different ways, similar to how an ORM like ActiveRecord define relationships for RDBMS systems.

```javascript
import { Model } from "active-mdx"

export class Epic extends Model {
  stories() {
    return this.hasMany("Story", {
      heading: "stories"
    })
  }
}

export class Story extends Model {
  epic() {
    return this.belongsTo("Epic", {
      id: () => this.meta.epic // will match epics with id of epics/this.meta.epic
    })
  }
}
```

An Epic MDX document with the heading depth 2 `## Stories` will treat all of the headings with depth 3 e.g. `### Story Title Subheading` underneath it as references to Story models

A Story MDX document with the YAML frontmatter

```markdown
---
epic: authentication
---
```

Will belong to an Epic instance with the id epics/authentication

Relationships can be used to build data structures from already existing documents, or to generate new documents from the content of another document.
