activerecord-hackery / ransack

Object-based searching.

Home Page:https://activerecord-hackery.github.io/ransack/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Doesn't work correctly with active multiple databases

defsdoor opened this issue · comments

I think that in lib/ransack/nodes/condition.rb where it is ducking out if the the connection is PostgreSQL -

return attr.attr if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"

It should be checking the connection of the query.

I have a setup with a postgres database and a SQLServer database and require the SQLServer searches to use LOWER but this condition assumes its a postgres database because that is the primary_abstract_class

A workaround is to wrap the ransack call from the controller inside

ActiveRecord::Base.connected_to ....
commented

@defsdoor thank you for the bug report. Can you add some detail about what the error is, and perhaps include some details how to replicate it, what version of Rails etc.

Does the problem occur across two different postgres databases?

The problem won't occur with 2 postgres databases because this condition is simply determining how to deal with case insensitive searches (assuming the 2 postgres dbs behave the same)

ActiveRecord::Base.connection returns the primary_abstract_class connection - but with multiple databases this may not reflect the database accessed by the query.

In my case there are postgreSQL and SQL Server databases.

      def attr_value_for_attribute(attr)
        return attr.attr if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"

        predicate.case_insensitive ? attr.attr.lower : attr.attr
      rescue
        attr.attr
      end

Ideally ransack should refer to the connection of the query and/or some other determination as, depending on the collation used, lower casing the search condition may not be necessary with an _i_cont search . At the moment though its determined purely from if its PostgreSQL or not.

If this isn't possible the workaround above works - Ransack sees the DB for the role specified - although its still unaware of collation or the need to lower case or not.