# Association.build doesn't make children visible immediately

**URL:** <https://www.ruby-forum.com/t/association-build-doesnt-make-children-visible-immediately/179820>\
**Category:** Rails\
**Created:** [December 4, 2009, 5:15pm UTC](https://www.ruby-forum.com/t/association-build-doesnt-make-children-visible-immediately/179820 "2009-12-04T17:15:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [December 4, 2009, 5:15pm UTC](https://www.ruby-forum.com/t/association-build-doesnt-make-children-visible-immediately/179820/1 "2009-12-04T17:15:36Z")

</div>

I have a problem with a has\_many :through association:

#in class Question  
has\_many :question\_assets, :class\_name =\> “MillionaireQuestionAsset”,  
:order =\> “position”, :dependent =\> :destroy  
has\_many :assets, :through =\> :question\_assets, :order =\>  
“millionaire\_question\_assets.position”

I have some validations on question which look at the children. In some  
other methods i’m building the association by building the join table  
entries, like so:

question.question\_assets.build(attributes)

(i’m building the join entries because there’s extra info in the join  
records which i need to stick in there)

Then when i go to save, the validations kick in. Unfortunately they are  
failing because they are testing question.assets, and even though  
question now ‘knows’ this it has question\_assets (ie the joins) it  
doesn’t yet ‘know’ that it has the assets - i need to save and reload  
the object in order for it to ‘realise’ that it now has the children  
assets. Except that i can’t save it because the validations prevent it  
being saved, because as far as the validations are concerned it doesn’t  
have any assets. arrghh.

I’m not sure how to deal with this. One option is to make the  
validation load up the assets manually, eg something like

assets = self.question\_assets.collect(&:asset)

and then test assets. But i just tried this and this breaks some other  
tests i have where i’m using

question.assets.build(attributes)

because _those_ tests don’t know about the new question\_asset  
associations. Ie they only know about the far-side children, they don’t  
know about the joins.

Any advice anyone? Is there a way to get the question to ‘refresh’  
somehow and realise that it has these assocations before saving?  
Obviously doing reload will wipe out all the unsaved stuff so that’s no  
good.
