Querying with Custom Joins

This is a companion discussion topic for the original entry at https://thoughtbot.com/upcase/videos/advanced-querying-custom-joins.

Question: Would #eager_load() work here? Or is there differing behavior that would make that a bad idea? My understanding is that eager_load uses LEFT OUTER JOIN.

…and as I keep watching the video my question is answered :grinning:

This is just a reminder that Rails 5, adds a left_outer_joins method.

Rails 5 also adds an or method which allows you to join clauses with OR, but I can’t find documentation, only this SO thread (and the pull request).

Obviously these still good demonstrations of how to write the minimum SQL that you need, just wanted to point out that for these common situations, there are now methods available.

1 Like

in Docs now ActiveRecord::QueryMethods :smile:

Hello! I’m a bit confused about the first challenge. Wouldn’t this have the same effect without the join?

Person.where("manager_id != ? OR manager_id IS NULL", Person.find_by!(name: "Eve"))

I’m probably missing something here.

:wave: this was a great post that helped me solve a complicated query.