【发布时间】:2016-05-12 15:00:16
【问题描述】:
在 Rails 4 应用程序中,我有相关模型申请人和商标,后者可使用 Searchkick 搜索:
class Applicant < ActiveRecord::Base
has_many :trademarks
end
class Trademark < ActiveRecord::Base
belongs_to :applicant
searchkick
end
我正在尝试查找没有申请人的商标实例。使用标准 ActiveRecord,以下查询有效,并在没有申请人的情况下返回商标:
Trademark.where(applicant: nil).count
(1.7ms) SELECT COUNT(*) FROM "trademarks" WHERE "trademarks"."applicant_id" IS NULL
=> 1
如何在添加 SearchKick 的情况下运行等效查询?
# these queries run correctly and show that SearchKick is working
Trademark.search "*" # => 7 records
Trademark.search "*", where: {status: "Removed"} # => 5 records
# When trying to search for applicant == nil, the search fails:
# this returns 7 records, instead of the 1 expected result
Trademark.search "*", where: {applicant: nil}
# this returns 0 records, instead of the 1 expected result
Trademark.search "*", where: {applicant_id: nil}
如何在 SearchKick 中使用 where 子句作为 nil 值?
【问题讨论】:
标签: ruby-on-rails rails-activerecord searchkick