Skip to content

It's not possible to chain #left_joins just like #joins method #34332

@dceluis

Description

@dceluis

Steps to reproduce

I noticed that it is possible to call a chain of joins method on a relation, and each call would append to the initial JOIN clause.

It's not possible to do so with left_joins, leading to very long method calls.

Expected behavior

Being able to chain left_joins calls, just like #joins.

Registration
  .left_joins(:rewards)
  .left_joins(' AND rewards.id = 0')
=> # SELECT * FROM registrations
=> # LEFT JOIN rewards
=> # ON rewards.registration_id = registrations.id
=> # AND rewards.id = 0'

Actual behavior

It raises an error.

Registration.left_joins(:rewards).left_joins(' AND rewards.id = 0')
=> # ArgumentError: only Hash, Symbol and Array are allowed.

One needs to write the complete initial left_joins clause.

Registration.joins('LEFT JOIN rewards ON registrations.id = rewards.registration_id AND rewards.id = 0')

#joins does not have this issue:

Registration.joins(:rewards).joins(' AND rewards.id = 0')
=> # SELECT * FROM registrations
=> # INNER JOIN rewards
=> # ON rewards.registration_id = registrations.id
=> # AND rewards.id = 0'

System configuration

Rails version: 5.2.1

Ruby version: 2.5.1

I could commit some time to contribute a PR for this if it's not a huge change (hopefully?).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions