I have this use case where I get symbolized deep associations from a specific model, and I have to fulfill certain requests that are related to using external connections. How to do this without resorting to writing full SQL manually?
Answers that I don't want: - use includes (doesn't allow deep associations very well (.includes (: cars => [: windows ,: engine => [: ignition] ..... works unexpectedly), and I don't want its side effects) - write SQL itself (sorry, this is 2013, support for cross-db, etc. etc.), and the objects that I get are read_only, more side effects)
I would like to have an Arel solution. I know that using isl_table from models I can build SQL expressions, there is also DSL for connections, but for some reason I can not use it in the joins method from the model:
car = Car.arel_table
engine = Engine.arel_table
eng_exp = car.join(engine).on(car[:engine_id].eq(engine[:id]))
eng_exp.to_sql
Car.joins(eng_exp)
Why this does not work, marries me. I don’t know exactly what is missing. But this is the closest to the solution that I have now. If someone can help me fill out my example or give me a nice workaround or tell me when Rails will enable such an explicitly needed feature, I will have my eternal thanks.
source
share