Module: Panda::Associations::ClassMethods
- Defined in:
- lib/panda/modules/associations.rb
Instance Method Summary collapse
- #has_many(relation_name) ⇒ Object
- #has_one(relation_name) ⇒ Object (also: #belongs_to)
Instance Method Details
#has_many(relation_name) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/panda/modules/associations.rb', line 25 def has_many(relation_name) # for example creates : @encodings ||= EncodingScope.new(self) define_method relation_name do model_name = "#{relation_name.to_s[0..-2].capitalize}" if instance_var = instance_variable_get("@#{relation_name}") instance_var else @associations ||= [] @associations << relation_name instance_variable_set("@#{relation_name}", Panda::const_get("#{model_name}Scope").new(self)) end end end |
#has_one(relation_name) ⇒ Object Also known as: belongs_to
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/panda/modules/associations.rb', line 9 def has_one(relation_name) # for example creates : @video ||= VideoScope.new(self) define_method relation_name do param_id = "#{relation_name}_id" if instance_var = instance_variable_get("@#{relation_name}") instance_var else @associations ||= [] @associations << relation_name instance_variable_set("@#{relation_name}", Panda::const_get(relation_name.to_s.capitalize).find(send(param_id.to_sym))) end end end |