Class: NaranyaEcm::Rest::Associations::HasOne
- Inherits:
-
SingularAssociation
- Object
- Association
- SingularAssociation
- NaranyaEcm::Rest::Associations::HasOne
- Defined in:
- lib/naranya_ecm/rest/associations.rb
Instance Attribute Summary
Attributes inherited from Association
#associated_class, #klass, #name, #options
Instance Method Summary collapse
-
#initialize(klass, name, options = {}) ⇒ HasOne
constructor
A new instance of HasOne.
- #macro ⇒ Object
Methods inherited from Association
Constructor Details
#initialize(klass, name, options = {}) ⇒ HasOne
Returns a new instance of HasOne.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/naranya_ecm/rest/associations.rb', line 71 def initialize(klass, name, = {}) super # Set the Getter: klass.send :define_method, name do value = self.instance_variable_get "@#{name}".to_sym if value.nil? reflection = self.class.reflect_on_association name foreign_key = (self.class.name.demodulize.underscore + '_id').to_sym value = self.instance_variable_set( "@#{name}".to_sym, reflection.associated_class.where(foreign_key => self.id).first ) end value end end |
Instance Method Details
#macro ⇒ Object
87 |
# File 'lib/naranya_ecm/rest/associations.rb', line 87 def macro; :has_one; end |