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
Inconsistent results with #or in ActiveRecord::Relation with respect to documentation #24055
Comments
@sikachu Should #order also be included in the list as it gives similar behaviour as above(when used with same parameters in both the relation) else it gives Just like #limit, #offset, or #distinct. |
That's covered by "they must differ only by #where" |
But do they work when they are added to both the relations. Not sure if documentation is complete. I think this case should be mentioned in documentation if this is supposed to work or we should stop passing in relation that has #limit, #offset, or #distinct. |
I recall that we wanted to reserve the right to make it more conservative in the future |
@sgrif So we want to stop passing in relation that has |
Why can't we allow :references? I have scopes I'm trying to combine together. Couple of points:
Just changing that method like this (add :references) works:
|
+1 for
then
gives expected SQL
gives seemingly compatible (have to test further) but different SQL and
gives exception
which i believe to be bug, since |
i just managed to work around this kinda well:
this gives me
idk if teh constraints i wanted to |
I was wondering what is the status of this issue? The PR that fixes the problem is rejected but this issue is still open, so is it going to be fixed or is it intended behaviour? Because I have the same problem as @ayghor but his work around isn't working for me and the only other solution I have is using raw sql but I like to avoid that as much as possible |
I was able to work around the problem using Arel with a solution based on @ayghor's example. A.joins(:b)
.where(
A.arel_table[:something_a].eq('xxx').or(B.arel_table[:something_b].eq('yyy'))
) |
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
Rails 5.2 I cannot OR two ActiveRecord::Relations which both have .limit applied to them:
This is contrary to the advice provided at https://makandracards.com/alexander-m/40723-support-or-in-active-record and at https://til.hashrocket.com/posts/d3b0c7acec-rails-5-use-limit-on-activerecords-with-or. The Guide is painfully brief on this topic: https://guides.rubyonrails.org/active_record_querying.html#or-conditions Is this a bug, and - if so - what's the likelihood of a fix? |
@sgrif Sean, do you think someone can take a look at this? It's biting a lot of people and makes |
|
Any progress on this issue? |
This comment has been minimized.
This comment has been minimized.
Hacky workaround: do all your That is, the following two lines will both fail A.joins(:b).where(b: b_query).or(A.where(query)) # error! 😞 A.where(query).or(A.joins(:b).where(b: b_query)) # error! 😞 but rearrange as follows, and you can evade the checker: A.where(query).or(A.where(b: b_query)).joins(:b) # works 😈 This works because all the checking happens inside the One downside of course is it doesn't read as nicely. |
@NAR8789 lol, I love it. |
After reading more in-depth and better-understanding some of the comments in this thread, I think the current behavior of It took me a long while to build enough personal context to parse that out of this thread though, so here are summaries by use case. Hopefully useful to others I want to
|
Thanks, @NAR8789. It's been two years since I touched any of this code so I'll have to dive back in to make any sense of it :) Your comment is really well-written. If you have any learning / reference material to recommend, I could use it! |
Example of making joins conditional in Rails 5: scope :complex_stuff, -> {
foo(joins: false).or(other_scope).joins_for_foo.any_other_joins_go_here
}
scope :foo, ->(joins: true) {
base = joins ? joins_for_foo : self
base.where("stuff")
}
scope :joins_for_foo, -> { joins(:bar) }
scope :other_scope, -> { where("other stuff") } I've also added a reminder to simplify this when this project has been updated to Rails 6.1+. |
Working on Rails 6.1.6.1, and it seems that the above is not true for me. What I'm trying to achieve is def a
SomeModel.where(foo: true)
end
def b
SomeModel.where(bar: true)
end
def c
SomeModel.joins(<<-SQL
INNER JOIN
...
SQL).where(something happens with the joined table)
end
I can get it to work by moving the join outside of |
@robmathews Do you know where below method can be added to rails application?
|
Looks like it might have been handled a long time ago. At least, reading down the thread, someone closed it and made a PR. |
Just tested this on Rails 7.x ( My case: joins(:employments).where(employments: { company: other_user.companies }).or(joins(:teams_users).where(teams_users: { project: other_user.projects })).joins(:teams_users).joins(:employments).distinct Fails with where(employments: { company: other_user.companies }).or(where(teams_users: { project: other_user.projects })).joins(:teams_users).joins(:employments).distinct Succeeds! |
According to documentation:
rails/activerecord/lib/active_record/relation/query_methods.rb
Line 650 in c577657
In order to use #or Neither relation may have a #limit, #offset, or #distinct set.
But, I could use all of the above like:
OR
Not sure if this is intended behaviour to support #limit, #offset, or #distinct.
If yes, then we need to improve the documentation about above use case.
If no, then should it be considered as bug?
I would be happy to look into it and send PR for either case.
But, wanted to confirm first for the intended behaviour.
The text was updated successfully, but these errors were encountered: