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"?