Skip to content
Looking for every option?See the full API reference →

Array ​

The Array field displays structured data in a paginated panel inside another resource. Use it for data supplied by a block or a model method, with an Array Resource defining the fields shown for each entry.

ruby
# app/avo/resources/course.rb
class Avo::Resources::Course < Avo::BaseResource
  def fields
    field :attendees, as: :array do
      [
        { id: 1, name: "John Doe", role: "Software Developer", organization: "TechCorp" },
        { id: 2, name: "Jane Smith", role: "Data Scientist", organization: "DataPros" }
      ]
    end
  end
end

Without a block, Avo calls the model method matching the field name, then falls back to the Array Resource's records method if no data is returned. The Array Resource returns an empty array by default and is read-only unless configured otherwise.

Set up the resource that renders each entry ​

Generate an Array Resource for the field. For field :attendees, as: :array, use:

bash
bin/rails generate avo:resource Attendee --array

Define the entry's fields in that resource:

ruby
# app/avo/resources/attendee.rb
class Avo::Resources::Attendee < Avo::Resources::ArrayResource
  def fields
    field :id, as: :id
    field :name, as: :text
    field :role, as: :text
    field :organization, as: :text
  end
end

Avo looks up the resource by the field name. If it has a different name, set use_resource to Avo::Resources::Attendee in the field declaration. See the Array Resource documentation for configuring the resource and enabling writes.

Supply data in a block ​

Return an array of hashes from the field's block, as in the opening example, to supply static or computed data. The block can read record to access the parent record. The Array Resource also accepts Active Record objects, an ActiveRecord::Relation, or StoreModel instances.

Allow access to the panel ​

Authorization

The Array field uses association-style authorization. If authorization is enabled and the panel is missing, check the parent resource's policy.

To explicitly authorize the attendees panel, define:

ruby
# app/policies/course_policy.rb
class CoursePolicy < ApplicationPolicy
  def view_attendees? = true
end

For more details, refer to the view_{association}? documentation.

Read data from the model ​

Declare field :attendees, as: :array without a block to read the data from Course#attendees:

ruby
# app/models/course.rb
class Course < ApplicationRecord
  def attendees
    User.first(6)
  end
end

Use an Array Resource whose fields match the objects returned by the method. For this example, define fields for the User attributes you want to display.

Provide a fallback dataset ​

If the field's block and the model method do not supply data, return a fallback dataset from the Array Resource's records method. Add this method to the resource that defines the entry's fields:

ruby
# app/avo/resources/attendee.rb
class Avo::Resources::Attendee < Avo::Resources::ArrayResource
  def records
    [
      { id: 1, name: "Default Attendee", role: "Guest", organization: "DefaultOrg" }
    ]
  end
end

Paginate at the source ​

Since avo >= v4.3.6

The block can read page and per_page, the page the panel is showing and its size. Return that page's records with the total to fetch one page at a time, as an Array Resource's records does:

ruby
# app/avo/resources/user.rb
class Avo::Resources::User < Avo::BaseResource
  def fields
    field :bookmarks, as: :array do
      {
        records: BookmarkStore.page(page, per: per_page, user_id: record.id),
        count: BookmarkStore.count(user_id: record.id)
      }
    end
  end
end

Give the resource that renders the field a find_record_method too, so a record's show page finds it outside the page the panel fetched.

count is the total number of matching records across all pages, not just the number returned for this page. BookmarkStore stands in for your data source; implement its page and count methods to match your storage.

Choose which data source wins ​

When using the Array field, Avo will fetch data in the following order:

  1. Use data returned by the block provided in the field.
  2. Fetch data from the associated model method (e.g., Course#attendees).
  3. Fall back to the records method defined in the resource.

Returning nil or false moves to the next source. Returning [] displays an empty panel instead of using the fallback.

Customize shared field behavior ​

To change the panel's initial page size, pass per_page. Set discreet_pagination to hide the pagination controls when all entries fit on one page.

To wait until someone requests the data, set loading to :manual. Add reloadable to let them refresh the panel without reloading the parent page.

The field inherits its panel behavior from association fields. See the Array field API for those options and shared field options for common configuration.