malomalo / activerecord-filter Goto Github PK
View Code? Open in Web Editor NEWA safe way to accept user parameters and query against your ActiveRecord Models
License: MIT License
A safe way to accept user parameters and query against your ActiveRecord Models
License: MIT License
Test committed
test/filter_relationship_test/belongs_to_polymorphic_test#::filter beyond polymorphic boundary
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.
Try in spire console:
Listing.filter(ownerships: {account_id: 5}).limit(50)
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))
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>'
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.