You're viewing Avo 2 docs — go to Avo 4

Skip to content

Add scopes to associations ​

Watch the demo video

When 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.

ruby
# 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_resource.rb
class UserResource < Avo::BaseResource
  # Before v2.5.0
  field :comments, as: :has_many, scope: -> { approved }
end

# app/avo/resources/user_resource.rb
class UserResource < Avo::BaseResource
  # After v2.5.0
  field :comments, as: :has_many, scope: -> { query.approved }
end

The comments query on the user Index page will have the approved scope attached.

Association scope

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.

All the has_many associations have the attach_scope option available too.