Module: Dynamoid::Associations::ManyAssociation
- Includes:
- Association, Enumerable
- Included in:
- HasAndBelongsToMany, HasMany
- Defined in:
- lib/dynamoid/associations/many_association.rb
Instance Attribute Summary
Attributes included from Association
Instance Method Summary collapse
-
#<<(object) ⇒ Dynamoid::Document
Add an object or array of objects to an association.
-
#==(other) ⇒ Boolean
Is this array equal to the association's records?.
-
#create(attributes = {}) ⇒ Dynamoid::Document|Array
Create a new instance of the target class, persist it and add directly to the association.
-
#create!(attributes = {}) ⇒ Dynamoid::Document|Array
Create a new instance of the target class, persist it and add directly to the association.
-
#delete(object) ⇒ Dynamoid::Document|Array
Delete an object or array of objects from the association.
-
#delete_all ⇒ Object
Deletes all members of the association and removes them from the association.
-
#destroy_all ⇒ Object
Destroys all members of the association and removes them from the association.
-
#include?(object) ⇒ Boolean
Delegate include? to the records.
- #initialize(*args) ⇒ Object
-
#where(args) ⇒ Dynamoid::Association
Naive association filtering.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
Delegate methods we don't find directly to the records array.
215 216 217 218 219 220 221 |
# File 'lib/dynamoid/associations/many_association.rb', line 215 def method_missing(method, *args) if records.respond_to?(method) records.send(method, *args) else super end end |
Instance Method Details
#<<(object) ⇒ Dynamoid::Document
Add an object or array of objects to an association.
tag.posts << post
tag.posts << [post1, post2, post3]
This preserves the current records in the association (if any) and adds the object to the target association if it is detected to exist.
It saves both models immediately - the source model and the target one so any not saved changes will be saved as well.
90 91 92 93 94 95 96 97 98 |
# File 'lib/dynamoid/associations/many_association.rb', line 90 def <<(object) associate(Array(object).collect(&:hash_key)) if target_association Array(object).each { |obj| obj.send(target_association).associate(source.hash_key) } end object end |
#==(other) ⇒ Boolean
Is this array equal to the association's records?
207 208 209 |
# File 'lib/dynamoid/associations/many_association.rb', line 207 def ==(other) records == Array(other) end |
#create(attributes = {}) ⇒ Dynamoid::Document|Array
Create a new instance of the target class, persist it and add directly to the association.
tag.posts.create(title: 'foo')
Several models can be created at once when an array of attributes specified:
tag.posts.create([{ title: 'foo' }, {title: 'bar'} ])
147 148 149 |
# File 'lib/dynamoid/associations/many_association.rb', line 147 def create(attributes = {}) self << target_class.create(attributes) end |
#create!(attributes = {}) ⇒ Dynamoid::Document|Array
Create a new instance of the target class, persist it and add directly to the association.
tag.posts.create!(title: 'foo')
Several models can be created at once when an array of attributes specified:
tag.posts.create!([{ title: 'foo' }, {title: 'bar'} ])
If the creation fails an exception will be raised.
130 131 132 |
# File 'lib/dynamoid/associations/many_association.rb', line 130 def create!(attributes = {}) self << target_class.create!(attributes) end |
#delete(object) ⇒ Dynamoid::Document|Array
Delete an object or array of objects from the association.
tag.posts.delete(post)
tag.posts.delete([post1, post2, post3])
This removes their records from the association field on the source, and attempts to remove the source from the target association if it is detected to exist.
It saves both models immediately - the source model and the target one so any not saved changes will be saved as well.
68 69 70 71 72 73 74 |
# File 'lib/dynamoid/associations/many_association.rb', line 68 def delete(object) disassociate(Array(object).collect(&:hash_key)) if target_association Array(object).each { |obj| obj.send(target_association).disassociate(source.hash_key) } end object end |
#delete_all ⇒ Object
Deletes all members of the association and removes them from the association.
tag.posts.delete_all
179 180 181 182 183 |
# File 'lib/dynamoid/associations/many_association.rb', line 179 def delete_all objs = target source.update_attribute(source_attribute, nil) objs.each(&:delete) end |
#destroy_all ⇒ Object
Destroys all members of the association and removes them from the association.
tag.posts.destroy_all
167 168 169 170 171 |
# File 'lib/dynamoid/associations/many_association.rb', line 167 def destroy_all objs = target source.update_attribute(source_attribute, nil) objs.each(&:destroy) end |
#include?(object) ⇒ Boolean
Delegate include? to the records.
49 50 51 |
# File 'lib/dynamoid/associations/many_association.rb', line 49 def include?(object) records.include?(object) end |
#initialize(*args) ⇒ Object
11 12 13 14 |
# File 'lib/dynamoid/associations/many_association.rb', line 11 def initialize(*args) @query = {} super end |
#where(args) ⇒ Dynamoid::Association
Naive association filtering.
tag.posts.where(title: 'foo')
It loads lazily all the associated models and checks provided conditions. That's why only equality conditions can be specified.
195 196 197 198 199 200 |
# File 'lib/dynamoid/associations/many_association.rb', line 195 def where(args) filtered = clone filtered.query = query.clone args.each { |k, v| filtered.query[k] = v } filtered end |