Class: ActiveRecord::Associations::Builder::SingularAssociation

Inherits:
Association
  • Object
show all
Defined in:
lib/active_record/associations/builder/singular_association.rb

Overview

:nodoc:

Direct Known Subclasses

BelongsTo, HasOne

Instance Attribute Summary

Attributes inherited from Association

#model, #name, #options, #reflection, #scope

Instance Method Summary collapse

Methods inherited from Association

build, #build, #configure_dependency, #define_readers, #define_writers, #initialize, #macro, #mixin, #valid_dependent_options, #validate_options

Constructor Details

This class inherits a constructor from ActiveRecord::Associations::Builder::Association

Instance Method Details

#constructable?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/active_record/associations/builder/singular_association.rb', line 7

def constructable?
  true
end

#define_accessorsObject



11
12
13
14
# File 'lib/active_record/associations/builder/singular_association.rb', line 11

def define_accessors
  super
  define_constructors if constructable?
end

#define_constructorsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_record/associations/builder/singular_association.rb', line 16

def define_constructors
  mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
    def build_#{name}(*args, &block)
      association(:#{name}).build(*args, &block)
    end

    def create_#{name}(*args, &block)
      association(:#{name}).create(*args, &block)
    end

    def create_#{name}!(*args, &block)
      association(:#{name}).create!(*args, &block)
    end
  CODE
end

#valid_optionsObject



3
4
5
# File 'lib/active_record/associations/builder/singular_association.rb', line 3

def valid_options
  super + [:remote, :dependent, :primary_key, :inverse_of]
end