class Syncano::ActiveRecord::Association::HasMany

Class for has many association

Attributes

associated_model[RW]
foreign_key[RW]
source[RW]
source_model[RW]

Public Instance Methods

<<(object) click to toggle source

Adds object to the related collection by setting foreign key @param [Object] object @return [Object]

# File lib/syncano/active_record/association/has_many.rb, line 40
def <<(object)
  object.send("#{foreign_key}=", source.id)
  object.save unless object.new_record?
  object
end
build() click to toggle source

Builds new associated object @return [Object]

# File lib/syncano/active_record/association/has_many.rb, line 27
def build
  associated_model.new(foreign_key => source.id)
end
create() click to toggle source

Creates new associated object @return [Object]

# File lib/syncano/active_record/association/has_many.rb, line 33
def create
  associated_model.create(foreign_key => source.id)
end
has_many?() click to toggle source

Checks if association is has_many type @return [TrueClass]

# File lib/syncano/active_record/association/has_many.rb, line 12
def has_many?
  true
end
scope_builder(source) click to toggle source

Returns new associaton object with source object set @param [Object] source @return [Syncano::ActiveRecord::Association::HasMany]

# File lib/syncano/active_record/association/has_many.rb, line 19
def scope_builder(source)
  association = self.dup
  association.source = source
  association
end

Private Instance Methods

method_missing(name, *args) click to toggle source

Overwritten method_missing for handling scope methods @param [String] name @param [Array] args

Calls superclass method
# File lib/syncano/active_record/association/has_many.rb, line 57
def method_missing(name, *args)
  scope_builder = Syncano::ActiveRecord::ScopeBuilder.new(associated_model).by_parent_id(source.id)

  if scope_builder.respond_to?(name) || !source.scopes[name].nil?
    scope_builder.send(name, *args)
  else
    super
  end
end