I have a little framework for dynamically constructing queries in rails (v6.1.7.3) based upon a json request from the front-end. As the use-cases keep growing, one thing that has been a bit of a pain to manage generically is having a way to keep track of which associations have already been joined to a given query scope as well as the alias used for those associations (if applicable)
Is there a built-in mechanism that I can use to determine if a given association has already been joined? And is there a means of determining an association's alias?
Thanks!
scope = MyModel.where(...)
...
scope.joins!(:assoc)
...
# how to determine if :assoc has already been joined to scope?
Rails do it work for you, you don't need to check if some association is already used:
Here
Array#|method is used. It returns the union of arrays, duplicates are removed:But you can check it with
joins_valuesor / andleft_outer_joins_valuemethod. These methods return array of associations (symbol array) and work such way:There are also
includes_values,eager_load_values,preload_valuesmethods: