Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.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
Sql ActiveRecord where语句不在(?)中,或不在';不存在_Sql_Ruby On Rails_Activerecord - Fatal编程技术网

Sql ActiveRecord where语句不在(?)中,或不在';不存在

Sql ActiveRecord where语句不在(?)中,或不在';不存在,sql,ruby-on-rails,activerecord,Sql,Ruby On Rails,Activerecord,在rails招聘应用程序中,我在下面进行查询,以选出适合某一特定工作的所有潜在用户 # Job Model def potentials User.includes(:source, :heat, :applications, common_app: [:cities, :industries]) .where('cities.id IN (?)', self.city_ids) .where('industries.id IN (?)', self.i

在rails招聘应用程序中,我在下面进行查询,以选出适合某一特定工作的所有潜在用户

  # Job Model
  def potentials
    User.includes(:source, :heat, :applications, common_app: [:cities, :industries]) 
      .where('cities.id IN (?)', self.city_ids)
      .where('industries.id IN (?)', self.industry_ids)
      .where('applications.id NOT IN (?)', self.applications.map(&:id))
      .order('common_apps.progress DESC')
  end
问题在于

.where('applications.id NOT IN (?)', self.applications.map(&:id))
我这样做是为了确保不会有已经申请工作的用户

但是,如果我这样写,查询将排除所有未申请任何作业的用户,因此没有applications.id


我该如何做到这一点,以便在where查询中,语句允许没有应用程序的用户使用

啊,找到了解决办法,但没有回答原来的问题

我没有在where子句中使用applications.id,而是使用users.id,这肯定存在于所有用户中

  def potentials
    User.includes(:source, :heat, :applications, common_app: [:cities, :industries]) 
      .where('cities.id IN (?)', self.city_ids)
      .where('industries.id IN (?)', self.industry_ids)
      .where('users.id NOT IN (?)', self.users.map(&:id))
      .order('common_apps.progress DESC')
  end
我想你想要:

.where("not exists(select * 
       from applications ap 
       where ap.user_id=users.id 
       and ap.job_id=?)", self.id)

当用户未申请任何作业时,应用程序id有什么价值?0,空白?嘿,Oscar,用户没有应用程序id。这更像是应用程序是用户和作业之间的连接模型。也就是说,用户通过::applications拥有许多:jobs,而job通过::applications拥有许多:应聘者,你能试着用
self.applications.map(&:id)
替换
self.applications.pluck(:id).presence | | |[-1]
吗?嘿,吉先生,运气不好。我可能把这个问题弄得有点模棱两可了。我担心的不是应用程序依赖于工作模型,而是用户模型。也就是说,self在本例中指的是一个作业,但给我们带来问题的是“applications.id”,因为在某些情况下,用户可能没有一个应用程序来处理sql并在Sequel Pro之类的应用程序上处理完整的sql查询,有时直接在原始sql中发现并出错更容易。