Class: ActiveRecord::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord/one/relation_ext.rb

Instance Method Summary collapse

Instance Method Details

#one!Object

Asserts that the collection contains exactly one record, and returns it.

If the collection is empty, it raises ActiveRecord::NoRecordFound. If the collection contains more than one record, it raises ActiveRecord::MultipleRecordsFound.



7
8
9
10
11
12
13
14
15
16
# File 'lib/activerecord/one/relation_ext.rb', line 7

def one!
  case size
  when 0
    raise ActiveRecord::NoRecordFound
  when 1
    first
  else
    raise ActiveRecord::MultipleRecordsFound
  end
end