Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • A administrate
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 96
    • Issues 96
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 32
    • Merge requests 32
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • thoughtbot, inc.
  • administrate
  • Issues
  • #563
Closed
Open
Issue created Apr 22, 2016 by Administrator@rootContributor

Specify scope with model in admin page?

Created by: jdell64

I have a user model with a role enum: [:student, :teacher, :admin]

In my administrate classroom dashboard, a classroom belongs to a teacher and has many students:

require "administrate/base_dashboard"

class ClassroomDashboard < Administrate::BaseDashboard

ATTRIBUTE_TYPES = {
    teacher: Field::BelongsTo.with_options(class_name: "User"),
    classroom_students: Field::HasMany,
    students: Field::HasMany.with_options(class_name: "User"),
    id: Field::Number,
    subject: Field::String,
    teacher_id: Field::Number,
    created_at: Field::DateTime,
    updated_at: Field::DateTime,
  }

....

FORM_ATTRIBUTES = [
    :subject,
    :teacher,
    :students,

  ]

  def display_resource(classroom)
    "#{classroom.subject} - #{classroom.teacher.first_name} #{classroom.teacher.last_name}"
  end

Everything works fine, but I'm wondering if I can make it to where my BelongsTo dropdown on the form can be limited using a scope I have in my User model User.teachers:

  # scopes
  scope :students, -> { where(role: User.roles[:student]) }
  scope :teachers, -> { where(role: User.roles[:teacher]) }
  scope :admins, -> { where(role: User.roles[:admin]) }

As it stands, the admin could make a student a teacher of a class (undesired). Whether or not it uses scopes, could I specify "This drop down contains model Users, where :type => :teacher"?

Assignee
Assign to
Time tracking