Sha256: ae5eeebc6b7320737479ff0a589c64256e0a4d65dca428837d0f35cf4cd8628f

Contents?: true

Size: 612 Bytes

Versions: 1

Compression:

Stored size: 612 Bytes

Contents

require "find_with_order/version"
require 'active_record'

class << ActiveRecord::Base
  def find_with_order(ids)
    return none if ids.blank?
    ids = ids.uniq
    return where(id: ids).order("field(#{table_name}.id, #{ids.join(',')})").to_a
  end
  def where_with_order(column, ids)
    return none if ids.blank?
    ids = ids.uniq
    return where(column => ids).order("field(#{column}, #{ids.map(&:inspect).join(',')})")
  end
end
unless ActiveRecord::Base.respond_to?(:none) # extend only if not implement yet
  class ActiveRecord::Base
    def self.none #For Rails 3
      where('1=0')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
find_with_order-1.0.1 lib/find_with_order.rb