I have 4 tables A, B, C & D as follows:
B belongs to A
B belongs to C
&
C has many D
As 'A' may or may not have any B's but if there is a B, then that B must have a C. And according to my logic, C will have at least one D or may be more.
Now, I want to get the list of A's with count(b) as well as count(d). Here count(b) and count(d) might be zero.
So what I was doing till now is as follows:
@a = A.joins(bs: {c: :ds}).select("a.*, count(b) as count_b, count(d) as count_d").group("a.id")
But eventually this is not working, as it INNER JOIN
b
with a
. Which means, if there is no corresponding b
for a
, then that a
will not be in the list @a
. That's the problem.
So, is there any way to do it ?