Permalink
Browse files

Implemented `create` method in HasMany association

  • Loading branch information...
1 parent 9979350 commit dd5c13531f155c91406c75a2dc9b649b313c445d @serebryakov serebryakov committed May 28, 2010
Showing with 9 additions and 11 deletions.
  1. +9 −11 lib/lib/associations/has_many.rb
@@ -77,25 +77,23 @@ 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
##
# Returns a new object of the collection type that has been
# 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
##

0 comments on commit dd5c135

Please sign in to comment.