Class: Superstore::Associations::BelongsTo

Inherits:
Association
  • Object
show all
Defined in:
lib/superstore/associations/belongs_to.rb

Instance Attribute Summary

Attributes inherited from Association

#owner, #reflection

Instance Method Summary collapse

Methods inherited from Association

#association_class, #association_class_name, #initialize, #loaded!, #loaded?, #reset, #target, #target=

Constructor Details

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

Instance Method Details

#belongs_to?Boolean

Returns:

  • (Boolean)


20
# File 'lib/superstore/associations/belongs_to.rb', line 20

def belongs_to?; true; end

#readerObject



4
5
6
7
8
9
10
# File 'lib/superstore/associations/belongs_to.rb', line 4

def reader
  unless loaded?
    self.target = get_record
  end

  target
end

#writer(record) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/superstore/associations/belongs_to.rb', line 12

def writer(record)
  self.target = record
  owner.send("#{reflection.foreign_key}=", record.try(reflection.primary_key))
  if reflection.polymorphic?
    owner.send("#{reflection.polymorphic_column}=", record.class.name)
  end
end