Add scopes to associations
Watch the demo videoWhen displaying has_many associations, you might want to scope out some associated records. For example, a user might have multiple comments, but on the user's Show page, you don't want to display all the comments, but only the approved ones.
# app/models/comment.rb
class Comment < ApplicationRecord
belongs_to :user, optional: true
scope :approved, -> { where(approved: true) }
end
# app/models/user.rb
class User < ApplicationRecord
has_many :comments
end
# app/avo/resources/user.rb
class Avo::Resources::User < Avo::BaseResource
def fields
field :comments, as: :has_many, scope: -> { query.approved }
end
endThe comments query on the user Index page will have the approved scope attached.
query already holds only the records the user's policy scope allows, so scope narrows the panel further without applying the policy again.

With version 2.5.0, you'll also have access to the parent record so that you can use that to scope your associated models even better.
Starting with version 3.12, access to resource and parent_resource was additionally provided.
All the has_many associations have the attach_scope option available too.