Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation 关联ID的Rails验证_Validation_Ruby On Rails 4_Activerecord - Fatal编程技术网

Validation 关联ID的Rails验证

Validation 关联ID的Rails验证,validation,ruby-on-rails-4,activerecord,Validation,Ruby On Rails 4,Activerecord,我正在构建一个Rails 4.2.4应用程序,在那里我有单位和医务人员。当我编辑每个单元时,我有两个医疗点,incharge和attendant。我需要某种方法来验证负责人id和助理id是否不相同。那样的话,我就不能在单位里同时担任两个职位 下面是我的模型和表单视图的外观 单位.rb class Unit < ActiveRecord::Base belongs_to :attendant, :foreign_key => :attendant_id, :class_name =

我正在构建一个Rails 4.2.4应用程序,在那里我有单位和医务人员。当我编辑每个单元时,我有两个医疗点,
incharge
attendant
。我需要某种方法来验证负责人id和助理id是否不相同。那样的话,我就不能在单位里同时担任两个职位

下面是我的模型和表单视图的外观

单位.rb

class Unit < ActiveRecord::Base
  belongs_to :attendant, :foreign_key => :attendant_id, :class_name => 'Medic'
  belongs_to :incharge, :foreign_key => :incharge_id, :class_name => 'Medic'
  belongs_to :unit_status
end
class Medic < ActiveRecord::Base
  has_many :units
end
<%= form_for(@unit) do |f|  %>
  <%= f.label 'Attendant'%>
  <%= f.collection_select(:attendant_id, Medic.order('name ASC'), :id, :name, {}) %>
  <%= f.label 'In Charge'%>
  <%= f.collection_select(:incharge_id, Medic.order('name ASC'), :id, :name, {}) %>
  <%= f.label 'Unit Status'%>
  <%= f.collection_select(:unit_status_id, UnitStatus.order("status ASC"), :id, :status, {})%>
  <%= f.submit "Update" %>
<% end %>
类单位:attendant\u id,:class\u name=>'Medic'
属于:incharge,:foreign\u key=>:incharge\u id,:class\u name=>“Medic”
属于:单位状态
结束
medic.rb

class Unit < ActiveRecord::Base
  belongs_to :attendant, :foreign_key => :attendant_id, :class_name => 'Medic'
  belongs_to :incharge, :foreign_key => :incharge_id, :class_name => 'Medic'
  belongs_to :unit_status
end
class Medic < ActiveRecord::Base
  has_many :units
end
<%= form_for(@unit) do |f|  %>
  <%= f.label 'Attendant'%>
  <%= f.collection_select(:attendant_id, Medic.order('name ASC'), :id, :name, {}) %>
  <%= f.label 'In Charge'%>
  <%= f.collection_select(:incharge_id, Medic.order('name ASC'), :id, :name, {}) %>
  <%= f.label 'Unit Status'%>
  <%= f.collection_select(:unit_status_id, UnitStatus.order("status ASC"), :id, :status, {})%>
  <%= f.submit "Update" %>
<% end %>
class-Medic
units/_form.html.erb

class Unit < ActiveRecord::Base
  belongs_to :attendant, :foreign_key => :attendant_id, :class_name => 'Medic'
  belongs_to :incharge, :foreign_key => :incharge_id, :class_name => 'Medic'
  belongs_to :unit_status
end
class Medic < ActiveRecord::Base
  has_many :units
end
<%= form_for(@unit) do |f|  %>
  <%= f.label 'Attendant'%>
  <%= f.collection_select(:attendant_id, Medic.order('name ASC'), :id, :name, {}) %>
  <%= f.label 'In Charge'%>
  <%= f.collection_select(:incharge_id, Medic.order('name ASC'), :id, :name, {}) %>
  <%= f.label 'Unit Status'%>
  <%= f.collection_select(:unit_status_id, UnitStatus.order("status ASC"), :id, :status, {})%>
  <%= f.submit "Update" %>
<% end %>

总之,如果我编辑了一个单位,并且我意外地将“1”的
id
分配给了该单位,我想出错并给出某种信息,“不能将同一个medic分配给两个位置”。差不多吧

我能想到的唯一一件事是,以某种方式过滤控制器中的参数,如果
attendant\u id
incharge\u id
的参数是==则重定向到edit\u unit\u路径并显示一条闪烁消息,“您不能将同一个medic分配到两个位置”


似乎最好在模型端进行验证,而不是在控制器中填充逻辑,但我不确定如何同时验证两个不同列的唯一性。

我在
单元
模型上提出了这一点

  validate :attendant_and_incharge

def attendant_and_incharge
  errors.add(:attendant_id, "can't be the same as the incharge") if attendant_id == incharge_id
end

这将不允许我将相同的id保存到机组模型中,以获得乘务员id和负责人id。它会以静默方式失败并指向机组路径。只需要在控制器中放入一些条件,在失败时重定向到编辑路径。(拇指支持)

我是在
装置
模型上想到这个的

  validate :attendant_and_incharge

def attendant_and_incharge
  errors.add(:attendant_id, "can't be the same as the incharge") if attendant_id == incharge_id
end

这将不允许我将相同的id保存到机组模型中,以获得乘务员id和负责人id。它会以静默方式失败并指向机组路径。只需要在控制器中放入一些条件,在失败时重定向到编辑路径。(ThumbsUp)

除了服务器端验证之外,最好禁止选定的医生出现在其他医生字段中,例如,动态填充下拉列表。@DaveNewton谢谢你的提示,理想情况下,这是我使用一些coffeescript/JS的目标,但我只是想先完成服务器端验证。:)除了服务器端验证之外,最好禁止选定的医生出现在其他医生字段中,例如,动态填充下拉列表。@DaveNewton谢谢你的提示,理想情况下,这是我使用一些coffeescript/JS的目标,但我只是想先完成服务器端验证。:)