Rails -Power of Joins

Muhammad Umair
Jun 12 · 2 min read

Rails way to join tables is simple yet very powerful. I am going to give you a few examples that would help you to understand the concept.

Suppose we have two tables namely user & projects.

Now the query to find all the projects for the users of type ‘writer’ would be:

See. So compact & simple.

Now, what if we need to join more than two tables?

Well let's consider the following example:

Now to fetch all projects with users of type ‘writer’ & publisher from country ‘USA’

Let's take another complex example:

Now the query to fetch all users with account_id ‘1’ & role name ‘admin’

Cheers !!!!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade