woahdae / active_record_or Goto Github PK
View Code? Open in Web Editor NEWAdds chaining scopes with 'or'
Adds chaining scopes with 'or'
User.where(hair_color: 'brunette', figure: 'slim').or.where(hair_color: 'blonde', figure: 'normal')
SELECT users
.* FROM users
WHERE ((users
.hair_color
= 'brunette' AND users
.figure
= 'slim' OR users
.hair_color
= 'blonde' AND users
.figure
= 'normal'))
Need to wrap both sides of the OR in parenthesis. This is the SQL what I expect:
SELECT users
.* FROM users
WHERE ((users
.hair_color
= 'brunette' AND users
.figure
= 'slim') OR (users
.hair_color
= 'blonde' AND users
.figure
= 'normal'))
I understand that the intentional behaviour for scopeless initial conditions is: "if you call or on a condition-less scope, it will be ignored", but I would say that is unexpected.
Imagine a scope that is sometimes pre-filtered (by CanCan in our case) for customers, but unfiltered for internal users. We want to show customers their products, and also offers. Here is our one line controller action. CanCan has provided products.
products = products.or.offers
This works for customers, but internal users can only see offers, as the initial scope is unfiltered and therefore dropped. I'd say this was unexpected...?
Although they shouldn't damage the logic, the extra parens sure are ugly.
If people find this project useful, it might be worth looking in to this.
Example from the tests:
to_blacklist.where_sql.must_equal(
"WHERE (((((artist_mentions_count > 0)"\
" OR (competition_rank > 7)))"\
" OR (subjective_dislike_rating > 3)))")
I know not whether this is a bug in squeel or active_record_or.
--- animal.rb ---
class Animal < ActiveRecord::Base
attr_accessible :name
scope :a, where { name.matches('%a%') & name.not_in(['ab']) }
scope :b, where("name LIKE '%b%'")
end
--- animal.rb ---
irb(main):082:0> Animal.a.or.b
Expected SQL:
SELECT "animals".* FROM "animals" WHERE ("animals.name LIKE '%a' AND "animals"."name" NOT IN ('abcd')) OR (name LIKE '%b%');
Actual SQL:
SELECT "animals".* FROM "animals" WHERE (("animals"."name" NOT IN ('ab') OR (name LIKE '%b%')))
The first sub-condition in scope :a was dropped (name LIKE '%a').
Hey @woahdae, I wanted to see if you are still maintaining the project, and plan to add the not
functionality along with or
.
For anyone else, who stumbles upon this who might have used a where Not A or Not B
solution - I'd love to understand how you approached it!
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.