Skip to content

Instantly share code, notes, and snippets.

@RichOrElse
Last active August 24, 2023 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichOrElse/c01c74c30766976f5587daaec2179239 to your computer and use it in GitHub Desktop.
Save RichOrElse/c01c74c30766976f5587daaec2179239 to your computer and use it in GitHub Desktop.
ActiveRecord missing methods
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
scope :subquery_as, ->(name) { unscoped.from(arel.as(name)) }
scope :select_with, ->(*fields) { dup.select_with!(*fields) }
scope :select_with!, ->(*fields) { self.select_values |= fields.flatten | [table[Arel.star]]; self }
end
module ArelExcept
refine ActiveRecord::Relation do
def except(other)= unscoped.from arel.except(other.arel).as(table_name)
end
[NilClass, FalseClass].each do |falsey_type|
refine falsey_type do
def except(other)= other
end
end
refine Module do
def except(other)= all.except other
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment