malomalo / activerecord-filter

A safe way to accept user parameters and query against your ActiveRecord Models

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fix filtering on OR and accessing same relation more than once

bemky opened this issue · comments

Action.filter([
    [
        {"subject_id"=>"ad959e17ed25475abdd9d818577d33b9"},
        "OR",
        {
            "metadata"=>{
                "key"=>{"eq"=>"project_id"},
                "value"=>{"eq"=>"ad959e17ed25475abdd9d818577d33b9"}
            }
        }
    ],
    "OR",
    {
        "metadata"=>{
            "key"=>{"eq"=>"subject_id"},
            "value"=>{"eq"=>"ad959e17ed25475abdd9d818577d33b9"}
        }
    }
]).to_sql

produces:

SELECT "actions".*
FROM "actions"
LEFT OUTER JOIN "metadata" ON "metadata"."event_id" = "actions"."event_id"
WHERE (
  (
    ("actions"."subject_id" = 'ad959e17ed25475abdd9d818577d33b9')
    OR
    ("metadata"."key" = 'project_id' AND "metadata"."value" = 'ad959e17ed25475abdd9d818577d33b9')
  )
  OR
  (
    "metadata_actions"."key" = 'subject_id'
    AND
    "metadata_actions"."value" = 'ad959e17ed25475abdd9d818577d33b9'
  )
)

notice metadata_actions is not intialized

Test added:
b6805a2