I have 3 models.
User.rb
has_many :user_cards
UserCard.rb
belongs_to :CardGroup
I want to get all users, with cards and the CardGroup information in one single query.
User.joins("LEFT OUTER JOIN user_cards ON user_cards.user_id = users.id AND user_cards.created_at BETWEEN '#{@start_time.to_s(:db)}' AND '#{@end_time.to_s(:db)}'")
.group('users.id, users.mobile, user_cards.user_id, user_cards.bin)
.select(
'users.id AS uid, users.name AS uname, users.mobile AS umobile,' \
'count(distinct(user_cards.id)) AS total_cards, \
'user_cards.bin AS card_bins'
)
Now, I want to join the third model CardGroup as well and get user_card.card_group.name in the above query.
How can I do it with nested join / any other way?