Skip to content

"WHERE (\"deleted_at\" IS NULL\)" doesn't persist though joins... #12

@jmervine

Description

@jmervine

"WHERE ("deleted_at" IS NULL)" doesn't persist though joins.

Below is the sample out of using a sub-select vs. a join along vs. a join with a deleted_at check for null.

[7] production(main)> AlarmMessage.count
=> 24110
[8] production(main)> Space.count
=> 2499
[9] production(main)> Space.with_deleted.count
=> 4479
[10] production(main)> AlarmMessage.where(space_id: Space.select(:space_id)).sql
=> "SELECT * FROM \"alarm_messages\" WHERE (\"space_id\" IN (SELECT \"space_id\" FROM \"spaces\" WHERE (\"spaces\".\"deleted_at\" IS NULL)))"
[11] production(main)> AlarmMessage.where(space_id: Space.select(:space_id)).count
=> 9157
[12] production(main)> AlarmMessage.join(:spaces, space_id: :space_id).sql
=> "SELECT * FROM \"alarm_messages\" INNER JOIN \"spaces\" ON (\"spaces\".\"space_id\" = \"alarm_messages\".\"space_id\")"
[13] production(main)> AlarmMessage.join(:spaces, space_id: :space_id).count
=> 13274
[14] production(main)> AlarmMessage.join(:spaces, space_id: :space_id).where(deleted_at: nil).sql
=> "SELECT * FROM \"alarm_messages\" INNER JOIN \"spaces\" ON (\"spaces\".\"space_id\" = \"alarm_messages\".\"space_id\") WHERE (\"deleted_at\" IS NULL)"
[15] production(main)> AlarmMessage.join(:spaces, space_id: :space_id).where(deleted_at: nil).count
=> 9157

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions