hardbap (owner)

Revisions

gist: 217246 Download_button fork
public
Public Clone URL: git://gist.github.com/217246.git
Embed All Files: show embed
AR#find with range support #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# allows you to find by Range (this method is in base.rb ln. 1559)
# ex.
# People.find(1..10)
# People.find(1, 3, 5..9)
def find_from_ids(ids, options)
  expects_array = ids.first.kind_of?(Array)
  return ids.first if expects_array && ids.first.empty?
 
# ids = ids.flatten.compact.uniq
  ids = ids.inject([]) {|m, o| m << (o.kind_of?(Range) ? o.to_a : o)}.flatten.compact.uniq
 
  case ids.size
  when 0
    raise RecordNotFound, "Couldn't find #{name} without an ID"
  when 1
    result = find_one(ids.first, options)
    expects_array ? [ result ] : result
  else
    find_some(ids, options)
  end
end