Sha256: 59c79d21326126cad0d053783eef4eeb321ad2433cf1a3d94f5b7b9b35a4d5a4
Contents?: true
Size: 1004 Bytes
Versions: 11
Compression:
Stored size: 1004 Bytes
Contents
# require "eitil_core/active_record/hash_to_relation" require "eitil_core/errors/raise_error" class Array def to_relation # Return an empty ActiveRecord_Relation instance when no objects are present, # instead of returning the empty array itself. The reason being, that when you # always return an association, you won't run into problems with undefined methods # called later on, expecting an association instead of an array. return ApplicationRecord.none unless self.present? unless self.all? { |item| item.class.ancestors.include? ApplicationRecord } raise_error "InvalidArrayError", ".to_relation requires that all array items are model instances" end unless self.each_cons(2).all? { |el1, el2| el1.class == el2.class } raise_error "InvalidArrayError", ".to_relation requires that all array items are instances of the same model" end _class = self.first.class ids = self.map(&:id) return _class.where(id: ids) end end
Version data entries
11 entries across 11 versions & 1 rubygems