Skip to content

attach_scope

Scope out the records the user sees on the Attach modal.

Default value ​

nil

Possible values ​

ruby
field :user,
  as: :belongs_to,
  attach_scope: -> { query.non_admins }

Pass in a block where you attach scopes to the query object and parent object, which is the actual record where you want to assign the association. The block is executed in the ExecutionContext.

query already holds only the records the user's policy scope allows, so building on it keeps that limit. A fresh relation, like -> { ::User.all }, widens the list past it. On every association, saving accepts exactly the records the block returns: those a widening scope adds are accepted, and a record outside a narrowing one is refused as if it didn't exist.

WARNING

The attach_scope will not filter the records in the listing from has_many or has_and_belongs_to_many associations. Use scope or a Pundit policy Scope for that.

ruby
field :members,
  as: :,
  attach_scope: -> { query.where.not(team_id: parent.id) }

In this example, in the attach_scope, we ensure that when attaching members to a team, only those who are not already members will appear in the list of options.