module ActiveRecord::Calculations
Normally find_by
is in FinderMethods, which older AR doesn't have
Public Instance Methods
find_by(*args)
click to toggle source
# File lib/duty_free.rb, line 137 def find_by(*args) where(*args).limit(1).to_a.first end
find_or_create_by(attributes, &block)
click to toggle source
# File lib/duty_free.rb, line 141 def find_or_create_by(attributes, &block) find_by(attributes) || create(attributes, &block) end
pluck(*column_names)
click to toggle source
# File lib/duty_free.rb, line 145 def pluck(*column_names) column_names.map! do |column_name| if column_name.is_a?(Symbol) && self.column_names.include?(column_name.to_s) "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}" else column_name end end # Same as: if has_include?(column_names.first) if eager_loading? || (includes_values.present? && (column_names.first || references_eager_loaded_tables?)) construct_relation_for_association_calculations.pluck(*column_names) else relation = clone # spawn relation.select_values = column_names result = if klass.connection.class.name.end_with?('::PostgreSQLAdapter') rslt = klass.connection.execute(relation.arel.to_sql) rslt.type_map = @type_map ||= proc do # This aliasing avoids the warning: # "no type cast defined for type "numeric" with oid 1700. Please cast this type # explicitly to TEXT to be safe for future changes." PG::BasicTypeRegistry.alias_type(0, 'numeric', 'text') # oid 1700 PG::BasicTypeRegistry.alias_type(0, 'time', 'text') # oid 1083 PG::BasicTypeMapForResults.new(klass.connection.raw_connection) end.call rslt.to_a elsif respond_to?(:bind_values) klass.connection.select_all(relation.arel, nil, bind_values) else klass.connection.select_all(relation.arel.to_sql, nil) end if result.empty? [] else columns = result.first.keys.map do |key| # rubocop:disable Style/SingleLineMethods Naming/MethodParameterName klass.columns_hash.fetch(key) do Class.new { def type_cast(v); v; end }.new end # rubocop:enable Style/SingleLineMethods Naming/MethodParameterName end result = result.map do |attributes| values = klass.initialize_attributes(attributes).values columns.zip(values).map do |column, value| column.type_cast(value) end end columns.one? ? result.map!(&:first) : result end end end
type_cast(v)
click to toggle source
# File lib/duty_free.rb, line 183 def type_cast(v); v; end