ransack icon indicating copy to clipboard operation
ransack copied to clipboard

Using ActiveRecord and Mongoid side by side is not possible

Open tmichel opened this issue 11 years ago • 10 comments

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

tmichel avatar Jan 23 '15 10:01 tmichel

Same problem here..

RobWu avatar Feb 02 '15 12:02 RobWu

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

RobWu avatar Feb 02 '15 12:02 RobWu

There are more places where these are conditionally required. Or just required overwriting other adapters.

tmichel avatar Feb 02 '15 12:02 tmichel

how did you solve these problem ,I am facing it now too?

remon avatar Mar 12 '15 21:03 remon

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

tmichel avatar Mar 13 '15 20:03 tmichel

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.

jonatack avatar Mar 31 '15 04:03 jonatack

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

Robert-Christopher avatar Apr 13 '15 12:04 Robert-Christopher

This should be fixed now by #541.

jonatack avatar Jul 26 '15 21:07 jonatack

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

ogerman avatar Dec 03 '15 14:12 ogerman

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:

jonatack avatar Dec 03 '15 14:12 jonatack