有多种条件的Rails

时间:2017-10-11 17:52:23

标签: ruby-on-rails activerecord where

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

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

我在患者/新视图上有一个Staff Doctor的下拉选择菜单,我想显示与当前用户属于同一所大学的员工用户列表,但我似乎无法让它工作。目前,下拉列表仅包含提示文本。我做错了什么?

患者/新观点:

<%= 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

1 个答案:

答案 0 :(得分:3)

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 %>

这不仅减少了很少的代码,而且将输入绑定到表单构建器可确保它们保持价值&#34;验证失败时。