You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Say you have a model where it has 2 belongs_to and both of those associations are the same model (e.g. User), any query you do that requires both will only use 1 join and override any where clauses set.
classInterview < BaseModel
table do
belongs_to interviewer : User
belongs_to interviewee : Userendend# I want to query for all interviews where the Interviewer isn't available for hire, but the Interviewee isInterviewQuery.new
.where_interviewer(UserQuery.new.available_for_hire(false))
.where_interviewee(UserQuery.new.available_for_hire(true))
In this case, we end up getting this SQL which is not what I want
My initial thought is if we just always alias the joins to your association method name, then maybe it'll fix this. However, I think that may also be a breaking change for anyone using where(raw_query : String).
So where_interviewer would go from
INNER JOIN users ON interviews.interviewer_id = users.id
to
INNER JOIN users AS interviewers ON interviews.interviewer_id = interviewers.id
But then anyone that was doing where("users.something @> ?", ...) would now get runtime errors since they would need to change it to where("interviewers.something @> ?", ...).
Maybe we can use some annotation to turn on this change at compile time and give people time to swap over? I'm not sure how that would work. 🤔
The text was updated successfully, but these errors were encountered:
The join part will be easy to do. where_whatever does auto inner joins by default, and we know what the association is at compile time here. However, the UserQuery.new("alias name") part is where it gets tricky. This query is built by you, so this query isn't known at compile time.
It's almost like I need something like
defwhere_whatever(some_query : BaseQuery)
new_query =BaseQuery.new("known_alias")
new_query.merge(some_query)
# do rest of stuffend
This is the part I'm foggy on and unsure of how to handle.
Related: #1007
Say you have a model where it has 2
belongs_to
and both of those associations are the same model (e.g. User), any query you do that requires both will only use 1 join and override any where clauses set.In this case, we end up getting this SQL which is not what I want
My initial thought is if we just always alias the joins to your association method name, then maybe it'll fix this. However, I think that may also be a breaking change for anyone using
where(raw_query : String)
.So
where_interviewer
would go fromto
But then anyone that was doing
where("users.something @> ?", ...)
would now get runtime errors since they would need to change it towhere("interviewers.something @> ?", ...)
.Maybe we can use some annotation to turn on this change at compile time and give people time to swap over? I'm not sure how that would work. 🤔
The text was updated successfully, but these errors were encountered: