Class: Her::Model::Associations::HasManyAssociation
- Inherits:
-
Association
- Object
- Association
- Her::Model::Associations::HasManyAssociation
- Defined in:
- lib/her/model/associations/has_many_association.rb
Instance Method Summary collapse
-
#build(attributes = {}) ⇒ Object
Initialize a new object with a foreign key to the parent.
-
#create(attributes = {}) ⇒ Object
Create a new object, save it and add it to the associated collection.
Methods inherited from Association
Instance Method Details
#build(attributes = {}) ⇒ Object
Initialize a new object with a foreign key to the parent
TODO: This only merges the id of the parents, handle the case
where this is more deeply nested
54 55 56 |
# File 'lib/her/model/associations/has_many_association.rb', line 54 def build(attributes = {}) @klass.build(attributes.merge(:"#{@parent.singularized_resource_name}_id" => @parent.id)) end |
#create(attributes = {}) ⇒ Object
Create a new object, save it and add it to the associated collection
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/her/model/associations/has_many_association.rb', line 73 def create(attributes = {}) resource = build(attributes) if resource.save @parent.attributes[@name] ||= Her::Collection.new @parent.attributes[@name] << resource end resource end |