Module: ActiveRecord::FindOnly

Defined in:
lib/active_record/find_only.rb,
lib/active_record/find_only/version.rb

Defined Under Namespace

Classes: TooManyRecords

Constant Summary collapse

VERSION =
"0.4.1"

Instance Method Summary collapse

Instance Method Details

#find_onlyObject

Gives the only record that matches the criteria. If no record matches the criteria, nil is returned. If more than one record matches the criteria, TooManyRecords is raised.



11
12
13
14
15
16
17
# File 'lib/active_record/find_only.rb', line 11

def find_only
  found_records = take(2)
  if found_records.length > 1
    raise TooManyRecords
  end
  found_records[0]
end

#find_only!Object

Gives the only record that matches the criteria. If no record matches the criteria, RecordNotFound is raised. If more than one record matches the criteria, TooManyRecords is raised.



22
23
24
# File 'lib/active_record/find_only.rb', line 22

def find_only!
  find_only || raise_record_not_found_exception!
end