Rails - Find All Database Records that Have No has_many Records

Mar 21, 2016

In Ruby-on-Rails, there is a handy one-liner that will allow you to find all the instances of one model that contain no instances of an associated model.

For example, lets say you have a model Blog that has a has_many relationship with Comments, and you come across a scenario where you want to find all blog posts that have no comments. The following line of code will do this for you:

  Blog.includes(:comments).where( :comments => { :blog_id => nil } )

One note about this line of code: the .includes() expects the name of the association between the models, while .where() expects the name of the table. If the Blog model had a has_one relation with comments, then the line would be:

  Blog.includes(:comment).where( :comments => { :blog_id => nil } )

Notes

Using .includes() lets you specify the relationships that you want included in your query results. This is usually better than a join, since it lets you access the attributes in the Comments model without performing an extra query[1].

Also, you can technically use any attribute in the Comment model and check if it's nil, since every field will return nil. Choosing the foreign key is preferred for readability.

Read More Posts