Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter values not working for Mongoid. #177

Closed
dennisfaust opened this issue Apr 27, 2015 · 4 comments
Closed

filter values not working for Mongoid. #177

dennisfaust opened this issue Apr 27, 2015 · 4 comments

Comments

@dennisfaust
Copy link
Contributor

We are trying to retrieve documents from Mongo using the filters business_id and person_id. With the filter values like this:

http://localhost:3000/api/v1/emails?filter[business_id]=123

Those are then each being converted to an array by CVS.parse_line in Resource#verify_keys like so:

'business_id' => ['123']

ActiveRecord will convert where(business_id: ['123']) to an in clause which is effectively an OR operation. Mongoid (and Mongo), however, will use the array as the value (since Mongo supports arrays as fields). This does not have the same semantics as an in clause in AR. As a result, our filters return no results because obviously we are not storing the business_id as an array.

We looked at tweaking this behavior in the verify_filter method but quickly realized that method is used for other purposes. Many tests broke after we applied the following:

      def verify_filter(filter, raw, context = nil)
        filter_values = case raw
        when String
          parts = CSV.parse_line(raw)
          parts.length > 1 ? parts : raw
        else
          raw
        end

        if is_filter_association?(filter)
          verify_association_filter(filter, filter_values, context)
        else
          verify_custom_filter(filter, filter_values, context)
        end
      end

We'd love to help out with a patch but could use some guidance on how you think this should be handled.

@bvandenbos
Copy link

Dup of #118

@dennisfaust
Copy link
Contributor Author

Here's a hack we're using if you run into this:

  def self.apply_filter(records, filter, value)
    if value.is_a?(Array)
      raise "Not Supported: value contains >1 element: #{value}" if value.length > 1
      value = value.first
    end
    records.where(filter => value)
  end

Obviously, this doesn't work if the filter does contain an array of elements.

@juanibiapina
Copy link

Updates on this?

@tommy-russoniello
Copy link
Collaborator

Duplicate of #118

@tommy-russoniello tommy-russoniello marked this as a duplicate of #118 Oct 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants