Exclusive join on has many relationship

Simon Thordal

I have a Postgres table where user can have many payment subscriptions and the payment subscriptions are either active or inactive, which is shown through an activated_at and an inactivated_at date field on the payment subscriptions table. I now want to find all the users without active payment subscriptions.

Finding all the users without any payment subscription is easy enough

User.joins("LEFT JOIN payment_subscriptions ON (payment_subscriptions.user_id = users.id)").where("payment_subscriptions.id IS NULL")

I also want to find all the users who only have inactive subscriptions. If I use the same pattern as above I will find all the users who at some point have inactivated their subscription, but there is no guarantee that they do not also have an active subscription.

User.joins("LEFT JOIN payment_subscriptions ON (payment_subscriptions.user_id = users.id)").where("payment_subscriptions.inactivated_at IS NOT NULL")

How do I get the users who have either no subscriptions or no active subscriptions from my table?

Roman Pekar

this one gives you users without any active subscriptions.

User.joins("LEFT JOIN payment_subscriptions on (payment_subscriptions.user_id = users.id and payment_subscriptions.inactivated_at is null)").where("payment_subscriptions.id is null")

If you want to get only those who have inactive, use also your previous join

I don't know if it possible in rails, but you also can use not exists syntax:

User.where('
    not exists (
         select *
         from payment_subscriptions as ps
         where ps.user_id = users.id and ps.inactivated_at is not null
    )
')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create join table record in Rails, has many: through relationship

From Dev

Rails field on join entity in has_many through relationship

From Dev

Many-to-many relationship children exclusive to parent model. How?

From Dev

JPA/Hibernate: Map many-to-many relationship when join table has own primary key

From Dev

Why I get 'JOIN' instead of 'LEFT JOIN' for 'has_many' relationship in DBIx::Class?

From Dev

Sqlalchemy one to many relationship join?

From Dev

Many to one relationship, unwanted JOIN

From Dev

Has many through relationship in rails

From Dev

Seeding with a has_many relationship

From Dev

MongoDB / Mongoose has many Relationship

From Dev

Rails has many through relationship

From Dev

JPQL left outer join on many to many relationship

From Dev

Laravel Complicated inner join on many to many relationship

From Dev

Oracle Recursive Join - Many to Many Relationship

From Dev

Inner Join Between Many to Many Relationship Models

From Dev

Ebean many to many relationship with join fails

From Dev

Rails: many to many relationship join table design

From Dev

How to JOIN tables in many-to-many relationship

From Dev

MySQL, join many to many relationship query

From Dev

Rails ActiveRecord : How can I give custom order for has_and_belongs_to_many relationship join table?

From Dev

inserting data into a table that has a many to many relationship

From Dev

How to join many to many relationship on max value for relationship field

From Dev

Rails Converting a has_many relationship into a has and belongs to many

From Dev

Mysql Left Join (1 to many relationship)

From Dev

Rails 4 has_many :through relationship: assign default value to join model attribute on create action from parent model

From Dev

Rails has_many relationship with prefilled views

From Dev

has_many relationship not working in a fixture

From Dev

Rails routing for has_and_belongs_to_many relationship

From Dev

has_many through relationship to the same Model

Related Related

  1. 1

    How to create join table record in Rails, has many: through relationship

  2. 2

    Rails field on join entity in has_many through relationship

  3. 3

    Many-to-many relationship children exclusive to parent model. How?

  4. 4

    JPA/Hibernate: Map many-to-many relationship when join table has own primary key

  5. 5

    Why I get 'JOIN' instead of 'LEFT JOIN' for 'has_many' relationship in DBIx::Class?

  6. 6

    Sqlalchemy one to many relationship join?

  7. 7

    Many to one relationship, unwanted JOIN

  8. 8

    Has many through relationship in rails

  9. 9

    Seeding with a has_many relationship

  10. 10

    MongoDB / Mongoose has many Relationship

  11. 11

    Rails has many through relationship

  12. 12

    JPQL left outer join on many to many relationship

  13. 13

    Laravel Complicated inner join on many to many relationship

  14. 14

    Oracle Recursive Join - Many to Many Relationship

  15. 15

    Inner Join Between Many to Many Relationship Models

  16. 16

    Ebean many to many relationship with join fails

  17. 17

    Rails: many to many relationship join table design

  18. 18

    How to JOIN tables in many-to-many relationship

  19. 19

    MySQL, join many to many relationship query

  20. 20

    Rails ActiveRecord : How can I give custom order for has_and_belongs_to_many relationship join table?

  21. 21

    inserting data into a table that has a many to many relationship

  22. 22

    How to join many to many relationship on max value for relationship field

  23. 23

    Rails Converting a has_many relationship into a has and belongs to many

  24. 24

    Mysql Left Join (1 to many relationship)

  25. 25

    Rails 4 has_many :through relationship: assign default value to join model attribute on create action from parent model

  26. 26

    Rails has_many relationship with prefilled views

  27. 27

    has_many relationship not working in a fixture

  28. 28

    Rails routing for has_and_belongs_to_many relationship

  29. 29

    has_many through relationship to the same Model

HotTag

Archive