Giter VIP home page Giter VIP logo

activerecord-filter's People

Contributors

bemky avatar malomalo avatar waratuman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

activerecord-filter's Issues

Filter by association count

If there was a way to do this without a counter cache column, that would be ideal. For example, how to query orders by how many have no items, 1 item, or more than 1 item.

Filtering using OR and through a relationship improperly requires relationship

Account.filter([{id: true}, 'OR', {projects: {id: true}}]).to_sql

produces:

SELECT \"accounts\".*
FROM \"accounts\"
INNER JOIN \"projects\" ON \"projects\".\"owner_id\" = \"accounts\".\"id\"
WHERE ((\"accounts\".\"id\" IS NOT NULL) OR (\"projects\".\"id\" IS NOT NULL))"

should use LEFT OUTER JOIN like:

SELECT \"accounts\".*
FROM \"accounts\"
LEFT OUTER JOIN \"projects\" ON \"projects\".\"owner_id\" = \"accounts\".\"id\"
WHERE ((\"accounts\".\"id\" IS NOT NULL) OR (\"projects\".\"id\" IS NOT NULL))

Filtering on Array causes issues

example:
Lead.filter(region_ids: {contains: 999})

stack trace:

TypeError: can't quote Array
	from /srv/core/shared/bundle/ruby/2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/quoting.rb:178:in `_quote'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/postgresql/quoting.rb:98:in `_quote'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/quoting.rb:23:in `quote'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/arel-7.1.4/lib/arel/visitors/to_sql.rb:800:in `quote'
	from /srv/core/shared/bundle/ruby/2.4.0/bundler/gems/arel-extensions-4f749032a0b6/lib/arel/visitors/postgresql_extensions.rb:14:in `visit_Arel_Nodes_Contains'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/arel-7.1.4/lib/arel/visitors/reduce.rb:13:in `visit'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/arel-7.1.4/lib/arel/visitors/to_sql.rb:825:in `block in inject_join'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/arel-7.1.4/lib/arel/visitors/to_sql.rb:821:in `each'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/arel-7.1.4/lib/arel/visitors/to_sql.rb:821:in `each_with_index'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/arel-7.1.4/lib/arel/visitors/to_sql.rb:821:in `each'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/arel-7.1.4/lib/arel/visitors/to_sql.rb:821:in `inject'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/arel-7.1.4/lib/arel/visitors/to_sql.rb:821:in `inject_join'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/arel-7.1.4/lib/arel/visitors/to_sql.rb:637:in `visit_Arel_Nodes_And'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/arel-7.1.4/lib/arel/visitors/reduce.rb:13:in `visit'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/arel-7.1.4/lib/arel/visitors/to_sql.rb:424:in `visit_Arel_Nodes_Grouping'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/arel-7.1.4/lib/arel/visitors/reduce.rb:13:in `visit'
... 19 levels...
	from /srv/core/shared/bundle/ruby/2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb:8:in `accept'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:12:in `to_sql'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:32:in `select_all'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/query_cache.rb:95:in `select_all'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/activerecord-5.0.2/lib/active_record/relation/calculations.rb:248:in `execute_simple_calculation'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/activerecord-5.0.2/lib/active_record/relation/calculations.rb:207:in `perform_calculation'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/activerecord-5.0.2/lib/active_record/relation/calculations.rb:121:in `calculate'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/activerecord-5.0.2/lib/active_record/relation/calculations.rb:40:in `count'
	from (irb):7
	from /srv/core/shared/bundle/ruby/2.4.0/gems/railties-5.0.2/lib/rails/commands/console.rb:65:in `start'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/railties-5.0.2/lib/rails/commands/console_helper.rb:9:in `start'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:78:in `console'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
	from /srv/core/shared/bundle/ruby/2.4.0/gems/railties-5.0.2/lib/rails/commands.rb:18:in `<top (required)>'
	from bin/rails:4:in `require'
	from bin/rails:4:in `<main>'

Fix filtering on OR and accessing same relation more than once

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.