ransack gem 是否适用于 MongoDB?
我根据我的情况定制了以下内容:
def index
@q = Person.search(params[:q])
@people = @q.result(distinct: true)
end
我遇到错误: nil:NilClass 的未定义方法“结果”
在这里https://github.com/ernie/ransack它描述了 ActiveRecord 并没有提到它只适用于 ActiveRecord ...
ransack gem 是否适用于 MongoDB?
我根据我的情况定制了以下内容:
def index
@q = Person.search(params[:q])
@people = @q.result(distinct: true)
end
我遇到错误: nil:NilClass 的未定义方法“结果”
在这里https://github.com/ernie/ransack它描述了 ActiveRecord 并没有提到它只适用于 ActiveRecord ...
直到 3 天前,您还不能将 Ransack 与 mongoid 一起使用,但现在有了这个拉取请求https://github.com/activerecord-hackery/ransack/pull/407是可能的。只需使用主分支
gem 'ransack', github: 'activerecord-hackery/ransack'
你可以像这样进行搜索
@q = Person.search(params[:q])
@people = @q.result # => Mongoid::Criteria
# or you can add more Mongoid queries
@people = @q.result.active.order_by(updated_at: -1).limit(10)
您可以在他们的适配器列表中看到他们只支持活动记录版本,我不熟悉那个 gem,但据我所知,它会在数据库上生成查询。由于 mongodb 不是关系数据库,它不理解 SQL,所以 gem 根本无法工作。
你可以试试Ransack Mongo。
# GET /people
def index
query = RansackMongo::Query.new
selector = query.to_query(params[:q])
@people = Person.where(selector)
end