Class: DatastaxRails::Associations::SingularAssociation

Inherits:
Association show all
Defined in:
lib/datastax_rails/associations/singular_association.rb

Overview

Encapsulates the common functionality between belongs_to and has_one relationships

Direct Known Subclasses

BelongsToAssociation, HasOneAssociation

Instance Attribute Summary

Attributes inherited from Association

#loaded, #owner, #reflection, #target

Instance Method Summary collapse

Methods inherited from Association

#aliased_column_family, #association_scope, #initialize, #klass, #load_target, #loaded!, #reload, #reset, #reset_scope, #scoped, #set_inverse_instance, #stale_target?, #target_scope

Constructor Details

This class inherits a constructor from DatastaxRails::Associations::Association

Instance Method Details

#build(attributes = {}, options = {}) {|record| ... } ⇒ Object

Yields:

  • (record)

24
25
26
27
28
29
# File 'lib/datastax_rails/associations/singular_association.rb', line 24

def build(attributes = {}, options = {})
  record = build_record(attributes, options)
  yield(record) if block_given?
  set_new_record(record)
  record
end

#create(attributes = {}, options = {}, &block) ⇒ Object


16
17
18
# File 'lib/datastax_rails/associations/singular_association.rb', line 16

def create(attributes = {}, options = {}, &block)
  create_record(attributes, options, &block)
end

#create!(attributes = {}, options = {}, &block) ⇒ Object


20
21
22
# File 'lib/datastax_rails/associations/singular_association.rb', line 20

def create!(attributes = {}, options = {}, &block)
  create_record(attributes, options, true, &block)
end

#reader(force_reload = false) ⇒ Object

Implements the reader method, e.g. foo.bar for Foo.has_one :bar


6
7
8
9
# File 'lib/datastax_rails/associations/singular_association.rb', line 6

def reader(force_reload = false)
  reload if force_reload || !loaded? || stale_target?
  target
end

#writer(record) ⇒ Object

Implements the writer method, e.g. foo.items= for Foo.has_many :items


12
13
14
# File 'lib/datastax_rails/associations/singular_association.rb', line 12

def writer(record)
  replace(record)
end