Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.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
Mysql 给定语句的ActiveRecord等效SQL_Mysql_Sql_Ruby On Rails_Postgresql_Activerecord - Fatal编程技术网

Mysql 给定语句的ActiveRecord等效SQL

Mysql 给定语句的ActiveRecord等效SQL,mysql,sql,ruby-on-rails,postgresql,activerecord,Mysql,Sql,Ruby On Rails,Postgresql,Activerecord,关联 预算 has_many :approvers 批准人 belongs_to :budget 要求 budgets .archived(false) .where("not exists (select 1 from approvers where approvers.budget_id = budgets.id)") .any? 检查预算是否有未分配的审批人 在其他领域(假设有3个预算) 如果所有3个预算的审批人计数均大于0,则应返回false 如果该预算中有任何一

关联

预算

has_many :approvers
批准人

belongs_to :budget
要求

budgets
  .archived(false)
  .where("not exists (select 1 from approvers where approvers.budget_id = budgets.id)")
  .any?
  • 检查预算是否有未分配的审批人
在其他领域(假设有3个预算)

  • 如果所有3个预算的审批人计数均大于0,则应返回false
  • 如果该预算中有任何一个有批准人
    这将返回所有未分配审批人的预算。

    谢谢Rishid,我们将尝试此解决方案。
    budgets
      .archived(false)
      .where("not exists (select 1 from approvers where approvers.budget_id = budgets.id)")
      .any?
    
    all_budgets_with_no_approvers = Budget.joins('left outer join approvers on budget.id = approvers.budget_id').where(approvers: { budget_id: nil })