Module: ActiveRepository::Associations::Methods
- Defined in:
- lib/active_repository/associations.rb
Instance Method Summary collapse
-
#belongs_to(association_id, options = {}) ⇒ Object
Defines “belongs to” type relation between ActiveRepository objects.
-
#has_many(association_id, options = {}) ⇒ Object
Defines “has many” type relation between ActiveRepository objects.
-
#has_one(association_id, options = {}) ⇒ Object
Defines “has one” type relation between ActiveRepository objects.
Instance Method Details
#belongs_to(association_id, options = {}) ⇒ Object
Defines “belongs to” type relation between ActiveRepository objects
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/active_repository/associations.rb', line 38 def belongs_to(association_id, = {}) = { class_name: association_id.to_s.classify, foreign_key: association_id.to_s.foreign_key }.merge() field [:foreign_key].to_sym belongs_to_methods(association_id, ) end |
#has_many(association_id, options = {}) ⇒ Object
Defines “has many” type relation between ActiveRepository objects
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/active_repository/associations.rb', line 15 def has_many(association_id, = {}) define_method(association_id) do = { class_name: association_id.to_s.classify }.merge() foreign_key = self.class.to_s.foreign_key klass = [:class_name].constantize klass.where(foreign_key => id) end end |
#has_one(association_id, options = {}) ⇒ Object
Defines “has one” type relation between ActiveRepository objects
29 30 31 32 33 34 35 |
# File 'lib/active_repository/associations.rb', line 29 def has_one(association_id, = {}) = { class_name: association_id.to_s.classify }.merge() has_one_methods(association_id, ) end |