ruby-on-railscollectionscollection-select

collect_select, I want to select users only with the admin role as true in my dropdown menu


<div class="field">
<%= form.label :user_id%>

<%= form.collection_select :user_id, User.all, :id , :emailaddress%>

How can I modify the select to only select the email address of users with their admin role as True?

Here is my schema..

create_table "courses", force: :cascade do |t|
t.integer "coursenumber"
t.integer "user_id"
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_courses_on_user_id"



create_table "users", force: :cascade do |t|
    t.string "firstname"
    t.string "lastname"
    t.string "title"
    t.integer "officenumber"
    t.string "emailaddress"
    t.integer "phone"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.boolean "admin"

Solution

  • I think you can use the where method, as you would normally:

    User.where(admin: true)

    <%= form.collection_select :user_id, User.where(admin: true), :id , :emailaddress%>

    You also could create a scope in your model named admins, and then just call User.admins or something like that.