# Does has\_many collection '\<\<' (append operator) always save/create child in DB when parent isn't ne

**URL:** <https://www.ruby-forum.com/t/does-has-many-collection-append-operator-always-save-create-child-in-db-when-parent-isnt-ne/169292>\
**Category:** Rails\
**Created:** [June 2, 2009, 12:21am UTC](https://www.ruby-forum.com/t/does-has-many-collection-append-operator-always-save-create-child-in-db-when-parent-isnt-ne/169292 "2009-06-02T00:21:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![zettabyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b19c9b/32.png) [@zettabyte](https://www.ruby-forum.com/u/zettabyte)\
**Post date:** [June 2, 2009, 12:21am UTC](https://www.ruby-forum.com/t/does-has-many-collection-append-operator-always-save-create-child-in-db-when-parent-isnt-ne/169292/1 "2009-06-02T00:21:12Z")

</div>

Howdy, the title mostly asks the question.

Given the following:

class Article \< ActiveRecord::Base ; has\_many :comments ; end  
class Comment \< ActiveRecord::Base ; belongs\_to :article ; end

I have an article already instantiated in the database, referenced as  
@article.

Will the following code always “create” (save to DB) a new comment?

@article.comments \<\< Comment.new :text =\> “Blah blah…”

I would like to be able to add comments to an article that already  
already exists (and may already have comments that exist) in the  
database w/out the new comment being saved to the database.

I would try this: @article.comments.build :text =\> “Blan blah…”

The reason I don’t is because my Comment class is sub-classed using  
STI. Thus, my code really looks more like this:

klass = Comment.get\_class(params[:comment\_type]) # get\_class returns  
Comment or a sub-type  
@article.comments \<\< klass.new :text =\> “Blah blah…”

Anyone have any ideas on how I can add STI comments to an article w/  
out saving them, doing so “the right way” (I’m sure I could ugly-hack  
it)?

Thanks.
