# HubDB filter using selectattr and foreign id for another table

**URL:** <https://community.hubspot.com/t/hubdb-filter-using-selectattr-and-foreign-id-for-another-table/32440>\
**Category:** CMS Development\
**Tags:** hubdb\
**Created:** [March 1, 2021, 3:06am UTC](https://community.hubspot.com/t/hubdb-filter-using-selectattr-and-foreign-id-for-another-table/32440 "2021-03-01T03:06:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bradhardinge](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/bradhardinge/32/75413_2.png) [@bradhardinge](https://community.hubspot.com/u/bradhardinge)\
**Post date:** [March 1, 2021, 3:06am UTC](https://community.hubspot.com/t/hubdb-filter-using-selectattr-and-foreign-id-for-another-table/32440/1 "2021-03-01T03:06:07Z")

</div>

I have a page displaying data from a HubDB table. It needs to be grouped by category. The categories are stored in another HubDB table. I can get this working using multiple hubdb\_table\_rows functions and filtering like below:

`hubdb_table_rows(table_id, '&category__contains=' ~ category)`

Problem is as there are more than 10 categories I hit the table scans limit. I’d prefer to do one hubdb\_table\_rows and use selectattr but I can’t get this working with a foreign id column type. Any ideas?

`rows|selectattr('category.name','contains','Category Name')`

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [March 1, 2021, 9:44pm UTC](https://community.hubspot.com/t/hubdb-filter-using-selectattr-and-foreign-id-for-another-table/32440/2 "2021-03-01T21:44:15Z")

</div>

@bradhardinge ,

Let me introduce you to @alyssamwilie . She is about as gifted as they come particularly around HubDB.

@alyssamwilie what say you?

---

<div class="post-metadata">

**Author:** ![alyssamwilie](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/alyssamwilie/32/2072_2.png) [@alyssamwilie](https://community.hubspot.com/u/alyssamwilie)\
**Post date:** [March 2, 2021, 6:55pm UTC](https://community.hubspot.com/t/hubdb-filter-using-selectattr-and-foreign-id-for-another-table/32440/3 "2021-03-02T18:55:12Z")

</div>

@bradhardinge

You could loop through the categories table, within that loop through your main table, and then use an if statement to find rows containing the currently looped category.

```lia-code-sample
{% set rows = hubdb_table_rows(tableid) %}
{% set categories = hubdb_table_rows(tableid) %}

{# Loop through categories table #}
{% for category in categories %}
 <h2>{{ category.name }}</h2>

 {# Loop through main table #}
 {% for row in rows %}
 {# Check if row contains category #}
 {% if category.name is within row.categories|map('name') %}
 <p>{{ row.title }}</p>
 {% endif %}
 {% endfor %}
{% endfor %}

```

---

<div class="post-metadata">

**Author:** ![bradhardinge](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/bradhardinge/32/75413_2.png) [@bradhardinge](https://community.hubspot.com/u/bradhardinge)\
**Post date:** [March 2, 2021, 10:45pm UTC](https://community.hubspot.com/t/hubdb-filter-using-selectattr-and-foreign-id-for-another-table/32440/4 "2021-03-02T22:45:57Z")

</div>

Thanks @alyssamwilie 🙂
