aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/singular_association.rb
blob: 023f7caf687a1ea24fb607834d7b42df606de56a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module ActiveRecord
  module Associations
    class SingularAssociation < AssociationProxy #:nodoc:
      def create(attributes = {})
        record = scoped.scoping { @reflection.create_association(attributes) }
        set_new_record(record)
        record
      end

      def create!(attributes = {})
        build(attributes).tap { |record| record.save! }
      end

      def build(attributes = {})
        record = scoped.scoping { @reflection.build_association(attributes) }
        set_new_record(record)
        record
      end

      private
        # Implemented by subclasses
        def replace(record)
          raise NotImplementedError
        end

        def set_new_record(record)
          replace(record)
        end
    end
  end
end