在rails

时间:2017-02-23 08:24:41

标签: ruby-on-rails-4 rails-activerecord

我有一个名为DueAction的表,它有一个DueEmployeeDetail的引用。我需要更新DueEmployeeDetail的is_confimed列,只有当DueAmployeeDetail表引用的DueAction表的所有条目都为true时才需要。例如。如果有5个条目且只有3个为真,那么is_confirmed不能更新为true。

in model i have written this :-

def is_exist(due_employee_detail)
  	# byebug
  	flag = 0
    DueAction.exists?(is_confirm: true,due_employee_detail_id: due_employee_detail)
    flag
  end

in controller i have wriien this:-

  if @due_action.is_exist(@due_action.due_employee_detail_id)
  DueEmployeeDetail.where(id: @due_action.due_employee_detail_id).update_all(is_confirmed: true)
  else
  end
  
  but its not working.Its checking only true not checking whether all are true or not.


  
  
  

1 个答案:

答案 0 :(得分:0)

我假设您对数据库模型设计感到满意。 如果我理解得当,DueAction会引用DueEmployeeDetail。您最好不要在控制器中执行此操作,您可以将此逻辑移至模型中。

class DueEmployeeDetail < ActiveRecord::Base

  before_save :confirm!, if: :has_all_due_actions_confirmed?

  def has_all_due_actions_confirmed?
    due_actions.all?(&:is_confirmed)
  end

  def confirm!
    self.is_confirmed = true
  end


end