Class: PassiveRecord::Associations::HasManyRelation

Inherits:
HasOneRelation show all
Extended by:
Forwardable
Includes:
Enumerable, PassiveRecord::ArithmeticHelpers
Defined in:
lib/passive_record/associations/has_many.rb

Direct Known Subclasses

HasManyThroughRelation

Instance Attribute Summary

Attributes inherited from Relation

#association, #parent_model

Instance Method Summary collapse

Methods included from PassiveRecord::ArithmeticHelpers

#average, #mode, #pluck, #sum

Methods inherited from HasOneRelation

#child_class, #child_class_name, #create, #id, #lookup, #lookup_or_create, #parent_model_id_field

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/passive_record/associations/has_many.rb', line 38

def method_missing(meth,*args,&blk)
  if child_class.methods.include?(meth)
    where.send(meth,*args,&blk)
  else
    super(meth,*args,&blk)
  end
end

Instance Method Details

#<<(child) ⇒ Object



29
30
31
32
# File 'lib/passive_record/associations/has_many.rb', line 29

def <<(child)
  child.send(parent_model_id_field + "=", parent_model.id)
  all
end

#allObject



19
20
21
# File 'lib/passive_record/associations/has_many.rb', line 19

def all
  child_class.where(parent_model_id_field => parent_model.id).all
end

#singular?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/passive_record/associations/has_many.rb', line 34

def singular?
  false
end

#where(conditions = {}) ⇒ Object



25
26
27
# File 'lib/passive_record/associations/has_many.rb', line 25

def where(conditions={})
  child_class.where(conditions.merge(parent_model_id_field.to_sym => parent_model.id))
end