Using ActiveRecord and Mongoid side by side is not possible
In our application we talk to different databases (namely PostgreSQL via ActiveRecord and mongodb via Mongoid).
The way adapters are structured renders ransack unusable with ActiveRecord when both gems are loaded. Adapters monkey patch base ransack classes to provide the specific behaviour needed. Mongoid is loaded last so that works, but nothing else.
There is a minimal test-case for reproducing this: link to gist
Same problem here..
The problem might be this lines:
if defined?(::Mongoid)
require 'ransack/adapters/mongoid/ransack/constants'
else
require 'ransack/adapters/active_record/ransack/constants'
end
https://github.com/activerecord-hackery/ransack/blob/master/lib/ransack.rb#L5-L9
There are more places where these are conditionally required. Or just required overwriting other adapters.
how did you solve these problem ,I am facing it now too?
@remon I haven't solved this. I had to pin the version to the last working one (1.5.1 in my case). IMO all the adapter related code has to be rewritten so it doesn't use monkey patching and can be loaded side by side.
Hm... perhaps the easiest solution would be to change the conditionals that check for Mongoid, to check for defined?(::Mongoid) && !defined?(::ActiveRecord)... which would at least allow having both defined and using Ransack for Active Record. And maybe DRY the conditionals into one place.
I just encountered this issue, do we have any work-around for this ? Looks like patching will require a re-write on a lot of files as pointed by @tmichel above
This should be fixed now by #541.
But it's still can't be used with both adapters, and condition still present here. https://github.com/activerecord-hackery/ransack/blob/master/lib/ransack/adapters.rb#L7
Using Active Record and Mongoid side by side is possible, in which case Ransack loads the AR adapter. Ransack does not load both adapters.
I'd be happy to look at your PR to run both adapters at the same time, if it's important to you. Please remember that none of the Ransack maintainers use Mongo DB, and that the PR to add the Mongoid adapter was accepted with the caveat that the community of Ransack's Mongoid users maintain it :wink: