DEV Community

Panayotis Matsinopoulos
Panayotis Matsinopoulos

Posted on

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

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…

</p>



?signin=true

Top comments (0)