You can see on their list of adapters that they only support active record versions, I am not familiar with that gem, but as fair as I understood, it generates queries on the database. As mongodb is not a relation database, it doesn't understand SQL, so the gem wont work at all.
Does ransack gem work for MongoDB? undefined method `result' for nil:NilClass
-
28-06-2022 - |
Question
Does ransack gem work for MongoDB?
I have customized below to my case:
def index
@q = Person.search(params[:q])
@people = @q.result(distinct: true)
end
I'm having error: undefined method `result' for nil:NilClass
Here https://github.com/ernie/ransack it describes about ActiveRecord and nothing is mentioned that it will work only for ActiveRecord...
Solution 2
OTHER TIPS
Until 3 days ago you couldn't use Ransack with mongoid, but now with this pull request https://github.com/activerecord-hackery/ransack/pull/407 it's possible. Just use the master branch
gem 'ransack', github: 'activerecord-hackery/ransack'
and you can make the search like
@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)
You could try Ransack Mongo.
# GET /people
def index
query = RansackMongo::Query.new
selector = query.to_query(params[:q])
@people = Person.where(selector)
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow