Module: Dynamoid::Associations::SingleAssociation
- Defined in:
- lib/dynamoid/associations/single_association.rb
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Is this object equal to the association’s target?.
-
#create(attributes = {}) ⇒ Dynamoid::Document
Create a new instance of the target class, persist it and associate.
-
#create!(attributes = {}) ⇒ Dynamoid::Document
Create a new instance of the target class, persist it and associate.
-
#delete ⇒ Object
Delete a model from the association.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, **kwargs, &block) ⇒ Object
Delegate methods we don’t find directly to the target.
72 73 74 75 76 77 78 |
# File 'lib/dynamoid/associations/single_association.rb', line 72 def method_missing(method, *args, &block) if target.respond_to?(method) target.send(method, *args, &block) else super end end |
Instance Method Details
#==(other) ⇒ Boolean
Is this object equal to the association’s target?
63 64 65 |
# File 'lib/dynamoid/associations/single_association.rb', line 63 def ==(other) target == other end |
#create(attributes = {}) ⇒ Dynamoid::Document
Create a new instance of the target class, persist it and associate.
post.logo.create(hight: 50, width: 90)
54 55 56 |
# File 'lib/dynamoid/associations/single_association.rb', line 54 def create(attributes = {}) setter(target_class.create(attributes)) end |
#create!(attributes = {}) ⇒ Dynamoid::Document
Create a new instance of the target class, persist it and associate.
post.logo.create!(hight: 50, width: 90)
If the creation fails an exception will be raised.
44 45 46 |
# File 'lib/dynamoid/associations/single_association.rb', line 44 def create!(attributes = {}) setter(target_class.create!(attributes)) end |
#delete ⇒ Object
Delete a model from the association.
post.logo.delete # => nil
Saves both models immediately - a source model and a target one so any unsaved changes will be saved. Doesn’t delete an associated model from DynamoDB.
30 31 32 33 34 |
# File 'lib/dynamoid/associations/single_association.rb', line 30 def delete disassociate_source disassociate target end |