Rails 有多个条件的地方

Rails where with multiple conditions

在我的应用程序中,我使用枚举定义了多个用户角色:

enum role: { staff: 0, clinician: 1, admin: 2 }

教职工用户各属于一所大学:

员工关注:

require 'active_support/concern'

module StaffUser
  extend ActiveSupport::Concern

  included do
    belongs_to :university
    has_many :patients
    has_many :referral_requests
    validates :university_id, presence: true, if: :staff?
  end

大学模式

class University < ApplicationRecord
  has_many :staffs, -> { where role: :staff}, class_name: "User"
  has_many :clinicians, through: :lists
  has_many :whitelists
  belongs_to :market

  validates :market_id, presence: true
end

我在 patients/new 视图上有一个用于 Staff Doctor 的下拉 select 菜单,我想在其中显示与当前用户属于同一所大学的教职工用户列表,但我可以似乎让它发挥作用。目前,下拉列表仅包含提示文本。我做错了什么?

patients/new 视图:

<%= form_for(@patient) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="checkbox">

    <h1>Tell us about your patient</h1>


    <h2>Insurance</h2>
    <% Insurance.all.each do |insurance| %>
      <%= check_box_tag "patient[insurance_ids][]", insurance.id, @patient.insurance_ids.include?(insurance.id), id: dom_id(insurance) %>
      <%= label_tag dom_id(insurance), insurance.name %><br>
    <% end %>

    <h2>Presenting Concerns</h2>
    <% Concern.all.each do |concern| %>
      <%= check_box_tag "patient[concern_ids][]", concern.id, @patient.concern_ids.include?(concern.id), id: dom_id(concern) %>
      <%= label_tag dom_id(concern), concern.name %><br>
    <% end %>

    <h2>Staff Doctor</h2>  

       <%= select_tag "patient[staff_doctor_id]", options_from_collection_for_select(User.where("role = ? AND university_id = ?", "staff", @user.university_id), "id", "name"), prompt: "Select this patient's therapist" %>

  </div>

  <%= f.submit "Submit", class: "btn btn-primary" %>
<% end %

患者控制器:

class PatientsController < ApplicationController
    before_action :require_login

def new
    @user = current_user
    @patient = current_user.patients.build
end

def index
    authorize Patient
    @patients = policy_scope(Patient)
end

def show
    @patient = Patient.find(params[:id])
end

def edit
    @patient = Patient.find(params[:id])
end

def update
    @patients = Patient.all
    @patient = Patient.find(params[:id])
    if @patient.update_attributes(patient_params)
        flash[:success] = "Patient Updated!"
        render 'patients/index'
    else
        render "edit"
    end
end


def create
    @patient = current_user.patients.build(patient_params)
    if @patient.save
        flash[:success] = "Patient Created!"
        redirect_to new_referral_request_path(patient_id: @patient.id)
    else
        Rails.logger.info(@patient.errors.inspect)
        render 'patients/new'
end
end


private

def patient_params
    params.require(:patient).permit(:age, :staff_doctor_id, :user_id, insurance_ids: [], gender_ids: [], concern_ids: [], race_ids: [])

end
end

ActiveRecord 中的范围是可链接的:

User.staff.where(university: @user.university)

链接 .where 或范围创建 AND 子句。所以所有条件都必须适用。

使用 ActiveRecord::Enum 为每个枚举状态创建范围。所以这相当于:

User.where(role: :staff, university: @user.university)

使用 ActiveRecord::Enum 时,您需要记住数据库存储的是整数而不是字符串:

User.where('role = 0') # staff
User.where('role = ?', User.statuses[:staff])

但是不需要为此查询使用 SQL 字符串。

创建选择和复选框的更好方法是 using the rails collection helpers:

<%= form_for(@patient) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="checkbox">
    <h1>Tell us about your patient</h1>
    <h2>Insurance</h2>
    <%= f.collection_check_boxes(:insurance_ids, Insurance.all, :id, :name) %>

    <h2>Presenting Concerns</h2>
    <%= f.collection_check_boxes(:concern_ids, Concern.all, :id, :name) %>

    <h2>Staff Doctor</h2>  
    <%= f.collection_select(:staff_doctor_id, User.staff.where(university: @user.university), :id, :name, prompt: "Select this patient's therapist") %>
  </div>
  <%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>

这不仅减少了很多代码,而且将输入绑定到表单构建器可确保它们 "hold the value" 在验证失败时。