Is there any way I can use find_by_sql AND eager load associations? I have to return everything in 1 sql statement, but I would still like to access associations normally when outputting in my rhtml file.
People succeed in answering Rich Collins's questions 37% of the time (32 successes in 86 attempts).
Answers by: Zachary Holt
Are you sure you need find_by_sql? You can generally get everything you need with the options to find, which will allow you to eagerly load associations, as you know.
I don't believe you can do find_by_sql and eager loading, in the Railsy way you want to. (You can always add columns to your ActiveRecord object from other tables, which allows you to access them as regular attributes, since that's all done through method_missing.)
I'm answering your question with a question--what are you doing that can't be done with the options to find?
You are right - it is not possible.
I was bummed out about that a while back. It would be really nice. But I understand the issues involved make it impractical. Have you looked into piggy-backing? Should work for 1-n relationships where you're dealing with an instance on the n side (i.e., only 1 associated object).
Yeah so was I. I don't like piggy backing. I usually just try to rethink what I am doing so I can use find :all with :include