# STI and has\_many

**URL:** <https://www.ruby-forum.com/t/sti-and-has-many/90825>\
**Category:** Rails\
**Created:** [April 2, 2007, 4:50am UTC](https://www.ruby-forum.com/t/sti-and-has-many/90825 "2007-04-02T04:50:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Russell\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Russell\_N](https://www.ruby-forum.com/u/Russell_N)\
**Post date:** [April 2, 2007, 4:50am UTC](https://www.ruby-forum.com/t/sti-and-has-many/90825/1 "2007-04-02T04:50:56Z")

</div>

I’m having a really hard time figuring out what’s wrong with this  
scenario.  
Here’s my setup

# My classes and their relationships…

class Document \< ActiveRecord::Base  
has\_many :attachments, :dependent =\> :destroy  
end

class Page \< Document  
end

class Entry \< Document  
end

class Attachment \< ActiveRecord::Base  
belongs\_to :document  
end

Then I’ve been trying this code [roughly]

@document = Page.find(1) # Assume there’s a record and not nil.  
@document.attachments # =\> []  
@document.attachments.create(params)  
@document.attachments # =\> [an\_attachment]

But the minute I reload…

@document = Page.find(1) # Reload that page!  
@document.attachments # =\> [] again!

It looks like my attachment isn’t ever getting a value for “document\_id”  
during the creation process. The attachment _is_ created but just sits  
there  
alone floating in space. I also tried polymorphic but that didn’t do any  
better. Here’s the setup I used for polymorphic…

class Document \< ActiveRecord::Base  
has\_many :attachments, :as =\> :attachable, :dependent =\> :destroy  
end

class Page \< Document  
end

class Entry \< Document  
end

class Attachment \< ActiveRecord::Base  
belongs\_to :attachable, :polymorphic =\> true  
end

Help!? Thanks in advance!

RSL
