如何过滤或范围内的医生只列出属于某个组织的医生?

时间:2012-08-11 15:59:58

标签: ruby-on-rails simple-form

创建约会时,为了说组织'ABC',我也可以看到属于其他组织的医生。它假设只列出来自'ABC'而非其他人的医生。我应该怎么做呢。

谢谢。

我的约会表格:

<%= simple_form_for(@appointment, :html => { :class => 'form-horizontal' }) do |f| %>
  <div class="form-inputs">
    <%= f.hidden_field :patient_id %>
    <%= f.association :physician, :label_method => :first_name, :include_blank => false, :as => :radio_buttons, :required => true %>
    <%= f.hidden_field :appointment_date, :value => DateTime.now  %>
    <%= f.hidden_field :organization_id, :value => current_user.organization_id%>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Create Appointment" %>
  </div>
<% end %>

我的模特:

/app/models/physician.rb

class Physician < ActiveRecord::Base
  has_many :appointments
  has_many :patients, :through => :appointments
  belongs_to :organization

  attr_accessible :physician_name, :organization_id
end

/app/models/appointment.rb

class Appointment < ActiveRecord::Base
  belongs_to :physician
  belongs_to :patient
  belongs_to :organization

  attr_accessible :physician_id, :patient_id, :appointment_date, :state, :organization_id
end

/app/models/patient.rb

class Patient < ActiveRecord::Base
  has_many :appointments
  has_many :physicians, :through => :appointments
  belongs_to :organization

  attr_accessible :patient_name, :organization_id
end

我的控制器:

/app/controllers/appointment_controller.rb

class AppointmentsController < ApplicationController
  def new
    @appointment = Appointment.new
    @appointment.patient_id = params[:patient_id]
  end

  def create
    @appointment = Appointment.new(params[:appointment])

    if @appointment.save
      flash[:notice] = "New appointment record created"
      redirect_to dashboards_path
    else
      render 'new'
    end
  end
end

1 个答案:

答案 0 :(得分:7)

这是因为simple_form不了解您的范围。如果你告诉它:

<%= f.association :phyisician %>

它只会列出数据库中所有可用的医生。

解决方案是给你想要展示的医生集合,例如你可以写:

<%= f.association :physician,
                  :collection => @appointment.patient.organization.physicians,
                  :label_method => :first_name,
                  :include_blank => false,
                  :as => :radio_buttons,
                  :required => true %>