Rails ActiveRecord混合条件where where和where.not

时间:2016-07-19 17:32:33

标签: ruby-on-rails ruby activerecord

我试图使用Rails 4.1.8和带有ActiveRecord的Ruby 2.1.5从我的数据库中选择一个记录子集。但结果包括我试图用where.not子句省略的记录。产生的sql也在下面说明。在我看来,where.not子句的sql被忽略或不正确。这样做的正确方法是什么?

class TablesController < ApplicationController

  def index
    @tables = Table.all.order("fil ASC").where(object: ['REP', 'CPT']).where.not(srcmbr: 'A*' )
  end

end

结果

Table Load (7.3ms)  SELECT inputfile.* FROM inputfile  WHERE inputfile.object IN ('REP', 'CPT') AND (inputfile.srcmbr != 'A*') ORDER BY fil ASC
  Rendered tables/index.html.erb within layouts/application (709.1ms)
Completed 200 OK in 912ms (Views: 882.4ms | ActiveRecord: 20.4ms)

2 个答案:

答案 0 :(得分:2)

A*代表模式吗?如果是这样,请尝试:

.where('srcmbr NOT LIKE ?', 'A%')

答案 1 :(得分:0)

.where('srcmbr NOT LIKE?','A%')

这解决了我的问题。