Rails 7 adds ActiveRecord::Relation#excluding


default-enum-in-rails

If you have not installed Rails 7 yet, please follow our other article on How to install rails 7 to do it in 10 seconds.

Rails 7 adds method excluding in ActiveRecord::Relation, which means you can now re-write the following code in much cleaner way.

Without Rails 7

# finds post by id 1
post_1 = Post.find(1) 

# Find all posts without post_1
Post.where.not(id: post_1.id)

With Rails 7

# finds post by id 1
post_1 = Post.find(1) 

# Find all posts without post_1
Post.excluding(post_1)


Recommended Posts

Rails 5 adds OR Activerecord method
This or method was introduced in Rails 5, It returns a new ActiveRecord::Relation which is...
Descendants vs Subclasses in Ruby
Descendants has been a part of Rails ActiveSupport::DescendantsTracker for quite a while. However, Class#subsclasses is...
Ruby 3.1 adds subclasses method
Ruby 3.1 adds Class#subclasses method, which returns an array of subclasses directly inheriting from the...