Sha256: 18bd9fd4e3ab3d19297b67443e904a1c03e248b9f38da9c33d201658719f5009

Contents?: true

Size: 970 Bytes

Versions: 1

Compression:

Stored size: 970 Bytes

Contents

---
title: Scoping HasMany Relations
---

To show a subset of a has_many relationship, create a new [has_many](https://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_many) relationship in your model (using the `scope` argument) and add it to the model's dashboard.

## Creating a scoped has_many relationship

Models can define subsets of a `has_many` relationship by passing a callable (i.e. proc or lambda) as its second argument.

```ruby
class Customer < ApplicationRecord
   has_many :orders
   has_many :processed_orders, ->{ where(processed: true) }, class_name: "Order"
```

Since ActiveRecord infers the class name from the first argument, the new `has_many` relation needs to specify the model using the `class_name` option.

## Add new relationship to dashboard

Your new scoped relation can be used in the dashboard just like the original `HasMany`.

```ruby
ATTRIBUTE_TYPES = {
  orders: Field::HasMany,
  processed_orders: Field::HasMany
```

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
administrate-1.0.0.beta3 docs/guides/scoping_has_many_relations.md