In 1-to-many Rails ActiveRecord relation, what is the best practice to get parent records that they only have children records?

p.matsinopoulos

I have a Post model that has many comments

class Post < ApplicationRecord
  has_many :comments
end

How do I get distinct posts that have comments excluding the posts that do not have comments?

I believe that Post.joins(:comments).distinct("posts.*") does the job.

Is there any better alternative?

I emphasize that the result should not have duplicate entries of posts, which is what happens when we join with comments (one-to-many relationship).

ReggieB

Looking at this again, and considering Mark's comment to my original solution, the simplest solution may just be to drop the "posts.*" from your original.

Post.joins(:comments).distinct

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ActiveRecord Rails: Get records using a condition in the relation

From Dev

How to get only 1 or 2 records on many to many table relation based on default column and passed language id

From Dev

Use activerecord to find records that are not in many-to-many relation for specific user (ruby-on-rails)

From Dev

SQL to find missing "child" records for each record in "parent" table of 1:many relation

From Dev

Rails scope for parent records that do not have particular child records

From Dev

Rails ActiveRecord: how to get non-duplicate child records by super-parent in view?

From Dev

Ruby on Rails, using activerecord to get all records that have a specified field containing a string in my array

From Dev

Get records from database only if children have at least one valid record

From Dev

Rails how to find records where the has_many relation exists?

From Dev

What is the best way for a shared database to have a multi-user login that only shows the records of logged in user?

From Dev

Finding parent records with children in LINQ

From Dev

Get records which don't have relation with specific id

From Dev

Rails 5.1 (postgresql): efficient DB query to select parent records and filtered children records

From Dev

best-practice for ordered many to many relation?

From Mysql

Get records in Many to Many query

From Dev

How to get unique children in a many to many relation?

From Dev

Sqlalchemy count of records in the relation of one to many

From Dev

Rails - only find records where has_many associated records are present

From Dev

Query parent relation when querying for child records

From Dev

ruby on rails how to get latest records from has many relationship

From Dev

Rails - get all records

From Dev

Delete both children and parent SQL records

From Dev

SUM operation on attributes of children of multiple parent records

From Dev

mysql query finding parent from children records

From Dev

SQLAlchemy query all children's records of a parent

From Dev

How does ActiveRecord::Relation get added to Rails' models and why does each model have individual Relation class?

From Dev

Only show records belonging to parent

From Dev

Rails 5 API - has_many through create action is returning 2 records although only 1 is persisted in the database

From Dev

Get records that have all sub records

Related Related

  1. 1

    ActiveRecord Rails: Get records using a condition in the relation

  2. 2

    How to get only 1 or 2 records on many to many table relation based on default column and passed language id

  3. 3

    Use activerecord to find records that are not in many-to-many relation for specific user (ruby-on-rails)

  4. 4

    SQL to find missing "child" records for each record in "parent" table of 1:many relation

  5. 5

    Rails scope for parent records that do not have particular child records

  6. 6

    Rails ActiveRecord: how to get non-duplicate child records by super-parent in view?

  7. 7

    Ruby on Rails, using activerecord to get all records that have a specified field containing a string in my array

  8. 8

    Get records from database only if children have at least one valid record

  9. 9

    Rails how to find records where the has_many relation exists?

  10. 10

    What is the best way for a shared database to have a multi-user login that only shows the records of logged in user?

  11. 11

    Finding parent records with children in LINQ

  12. 12

    Get records which don't have relation with specific id

  13. 13

    Rails 5.1 (postgresql): efficient DB query to select parent records and filtered children records

  14. 14

    best-practice for ordered many to many relation?

  15. 15

    Get records in Many to Many query

  16. 16

    How to get unique children in a many to many relation?

  17. 17

    Sqlalchemy count of records in the relation of one to many

  18. 18

    Rails - only find records where has_many associated records are present

  19. 19

    Query parent relation when querying for child records

  20. 20

    ruby on rails how to get latest records from has many relationship

  21. 21

    Rails - get all records

  22. 22

    Delete both children and parent SQL records

  23. 23

    SUM operation on attributes of children of multiple parent records

  24. 24

    mysql query finding parent from children records

  25. 25

    SQLAlchemy query all children's records of a parent

  26. 26

    How does ActiveRecord::Relation get added to Rails' models and why does each model have individual Relation class?

  27. 27

    Only show records belonging to parent

  28. 28

    Rails 5 API - has_many through create action is returning 2 records although only 1 is persisted in the database

  29. 29

    Get records that have all sub records

HotTag

Archive