ActiveRecord::Relation new features in Rails 5


After all this time, we finally have the #or method in ActiveRecord. We can call #or on a relation and pass another relation as its argument and it will generate an OR query. We can use it like this:

class Issue < ApplicationRecord  
  scope :reported, -> { where(status: 'reported') }  
  scope :open,     -> { where(status: 'open') }
active_issues = Issue.reported.or(


The new #in_batches method yields a relation, unlike #find_in_batches which yields an array. We can use this method for things like this -

Person.where('age >= 18').in_batches(of: 1000) do |people|  
  people.update_all(can_vote: true)
comments powered by Disqus