-
Notifications
You must be signed in to change notification settings - Fork 51
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
Boolean column reference without operator is only sometimes supported #129
Comments
Thank you for the detailed request. I agree that this does feel a bit inconsistent, and it'd be nice to fix it in some way. What behavior would you expect for a non-boolean column? Post.where.has { title } I think what we're seeing in your examples is just Arel's behavior. So, for example:
I haven't really looked into this, but I suspect that it might be a less invasive change to support usage like this (note the question mark): Author.where.has { ugly? } I'm open to other solutions, too. Would you be willing to put together a PR? If not, that's totally okay! |
Thanks for the quick reply @rzane and for the Arel demonstration! For any of Post.where.has { title }
Post.where.has { title & ugly}
Post.where.has { ugly & title } I think I'd expect an error to be raised - perhaps something along the lines of I like how
I'm happy to work on a PR, if we can decide what the desired change is 😄 |
I'm not opposed to this as a proposal. I'd be okay with raising an error for non-boolean attributes, though I could also see an argument for: Post.where.has { title }
# SELECT * FROM posts WHERE title IS NOT NULL I think we might also want to support: Author.where.has { !ugly } You already hit on this, but this usage might complicate your proposal because Author.selecting { ugly }
Author.where.has { ugly } So, I suggest resolving Here are a few places to look:
|
Interesting report. For me it seems, that this is an AREL feature / bug and was never an intentional feature of baby_squeel. If I read it correct it would be best to raise an error in @owst Is it possible to "fix" your code and add
I would vote against this. Because normally methods ending with a question mark return true or false.
That might not be so easy beacause |
To explicitly allow all of: ```ruby Post.where.has { published } Post.where.has { published & (view_count >= 1) } Post.where.has { (view_count >= 1) & published } ``` Fixes rzane#129
To explicitly allow all of: ```ruby Post.where.has { published } Post.where.has { published & (view_count >= 1) } Post.where.has { (view_count >= 1) & published } ``` Fixes rzane#129
@rzane @rocket-turtle - thanks for the thoughts so far - I've created a draft PR (#131) that explicitly supports "plain references" to attributes, coercing boolean attributes to |
The MR seems to be a good start. Corrosponding to I still don't think the comlexety is worth the gain of writing |
Thanks @rocket-turtle. My main concern is the "only sometimes works" aspect of this issue, rather than particularly allowing plain boolean attribute references at the top-level of a
Any thoughts/preference between those? FYI I've already changed our code to |
If 3 is even more complex then 2 I would prefere 1 maybe with a documentation in the Readme and a Test so we could remove the hint if arel fixes something on there side. 4 would be the best. The Documentation of the bug is realy good and could probably be transfered to AR only. But I don't know if they think its a bug or a misuse of AR. |
Issue
In some contexts, a boolean column can be referenced without an explicit operator, but in others an exception is raised. I'm not sure that this is intentionally allowed in any context, but it would be nice if it were supported for convenience.
Setup
If we have a table:
then we can query for all published posts with:
and can query for only viewed published posts with:
and equivalently, we can swap the order of the conditions:
Issue demonstration
Our inner Rubyist might not like the explicit
published == true
comparison in the first compound query, so we successfully rewrite to:but if we try the same rewrite on the reordered condition then an exception is raised:
similarly if we try to filter for all published posts without explicit comparison a (different) error is raised:
Expected behaviour
Either that a (boolean) column reference without explicit operator is always supported, or always raises an error. So either all of:
are supported and operate in the expected way, or all raise an error.
Perhaps a reference without operator to a boolean column should generate an implicit
(column == true)
node rather than being passed through as aArel::Attributes::Attribute
? A bare reference to a non-boolean column should raise an explicit error.Reproduction
The text was updated successfully, but these errors were encountered: