Find all records with at least one child in rails

### Find all records with at least one child
Parent.join(:children).uniq.all
# or 
Parent.includes(:children).where("children.id IS NOT NULL")
# or Rails 5
Parent.left_outer_joins(:children).where.not(children: {id: nil})


### Find all records with no children
Parent.includes(:children).where(children: {id: nil})
# or Rails 5
Parent.left_outer_joins(:children).where(children: {id: nil})

Comments

  1. Influx Infotech is Web Designing Company in India with the team of Expert Web Designers. We are developing Websites Website Design Company in India which loved by our clients.
    Web Design Agra
    www.influxinfotech.com

    ReplyDelete

Post a Comment

Popular Posts

How to pass hash in Postman

nginx: unrecognized service

Reading Excel Sheets using "Roo" gem in ruby on rails

Bootstrap Select Picker append add new item if search not exist

Add CORS to Nginx on AWS Elastic Beanstalk

Enable gzip compression on Elastic Beanstalk with nginx

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

Get video duration by URL in Ruby on Rails

site-enables nginx setting in ruby in rails