Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented `create` method in HasMany association

  • Loading branch information...
commit dd5c13531f155c91406c75a2dc9b649b313c445d 1 parent 9979350
@serebryakov serebryakov authored
Showing with 9 additions and 11 deletions.
  1. +9 −11 lib/lib/associations/has_many.rb
View
20 lib/lib/associations/has_many.rb
@@ -77,16 +77,13 @@ def find(*args)
end
##
- # Returns one or more new objects of the collection type that
- # have been instantiated with attributes and linked to this object
- # through a foreign key, but have not yet been saved.
+ # Returns a new object of the collection type that has been
+ # instantiated with attributes and linked to this object,
+ # but not yet saved.
##
- def build(*attributes)
- objs = attributes.map do |atts|
- @association.new(atts)
- end
- self.push(*objs)
- objs
+ def build(attributes)
+ obj = @association.new(attributes)
+ (self << obj).last
end
##
@@ -94,8 +91,9 @@ def build(*attributes)
# instantiated with attributes, linked to this object through
# a foreign key, and that has already been saved.
##
- def create
- raise NotImplementedError, '`create` not yet implemented' # TODO
+ def create(attributes = {})
+ obj = @association.create(attributes.merge({ reverse => @instance }))
+ (self << obj).last
end
##
Please sign in to comment.
Something went wrong with that request. Please try again.