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...

Was it helpful?

Solution 2

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.

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
scroll top