Rails 4.1.8とRuby 2.1.5とActiveRecordを使用してデータベースからレコードのサブセットを選択しようとしています。しかし、結果にはwhere.not節で省略しようとしているレコードが含まれています。結果として得られるSQLも以下に記載されています。 where.not句のSQLが無視されているか、間違っていると思われます。これを行う適切な方法は何ですか?Rails ActiveRecordは、whereとwhere.notの混合条件ではありません。
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)
「A *」はパターンマッチングを表していますか?そうでない場合は、 '.where( 'srcmbr NOT LIKE?'、 'A%')' – maicher
ちょうど注記: 'Table.where(オブジェクト:['REP'、 'CPT'])。where.not(srcmbr : 'A *').order(fil::asc) '。とにかくクエリで何が間違っていますか?スジは通ってるようだ。 – potashin
「A *」と一致する項目が結果に表示されます。 –