-
Notifications
You must be signed in to change notification settings - Fork 21.6k
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
Fix find_by
with ActiveRecord::Base
object
#26213
Conversation
r? @arthurnn (@rails-bot has picked a reviewer for you, use r? to override) |
hash = args.first | ||
|
||
def find_by(hash, *args) # :nodoc: | ||
return super if scope_attributes? || !(Hash === hash) || reflect_on_all_aggregations.any? | ||
return super if hash.values.any? { |v| | ||
v.nil? || Array === v || Hash === v || Relation === v |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noted in passing: this sounds like it would let a Range through, but the below seems ill-equiped to handle that.
We expliclty didn't want to users to use active record instances in find methods we even have deprecations to that in 4.2 https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/core.rb#L139. The proper fix to this is raise an exception. |
We have a Post.find_by(author_id: author, type: ["Post", "SpecialPost"]) Should we raise an exception for |
That seems weird. In 4.2 we told people to not use AR instances in finder methods, now we are allowing again. I believe that case is a left over of @tenderlove's work to deprecate support of AR instances in finders. @tenderlove WDYT about this? |
This deprecation was added to Essay.find_by(writer: author)
# => SELECT "essays".* FROM "essays" WHERE "essays"."writer_type" = 'Author' AND "essays"."writer_id" = 'David' LIMIT ? [["LIMIT", 1]] |
oh, I see what you mean. Yeah it makes sense. I'll think about a bit before taking the decision to merge or not. r? @rafaelfranca |
I think something like:
is be OK. What I'd really like to eliminate is
AR objects aren't ids, so it doesn't make sense. |
Actually https://github.com/rails/rails/blob/v5.0.0/activerecord/lib/active_record/relation/finder_methods.rb#L78 Should we deprecate |
IMO, no. Unlike |
I think that it should be consistent behavior both
--- a/activerecord/lib/active_record/core.rb
+++ b/activerecord/lib/active_record/core.rb
@@ -205,7 +205,7 @@ def find_by(*args) # :nodoc:
hash = args.first
return super if hash.values.any? { |v|
- v.nil? || Array === v || Hash === v || Relation === v
+ v.nil? || Array === v || Hash === v || Relation === v || Base === v
}
|
Long term I'd actually like to reuse the handlers from the predicate builder as part of the statement cache, as well as provide a point of extension for the connection so we can do things like change arrays to use Short term though I think the second option makes the most sense as the temporary stop-gap |
The alternative of rails#26213. Currently `find_by` and `where` with AR object return inconsistent result. This is caused by statement cache does not support AR object. Passing to finder method to fix the issue. Fixes rails#26210.
Closing in favor of #27564 |
Fixes #26210.