# Page\_attachments with page type not set to normal

**URL:** <https://www.ruby-forum.com/t/page-attachments-with-page-type-not-set-to-normal/122255>\
**Category:** Radiant CMS\
**Created:** [December 5, 2007, 11:08pm UTC](https://www.ruby-forum.com/t/page-attachments-with-page-type-not-set-to-normal/122255 "2007-12-05T23:08:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [December 5, 2007, 11:08pm UTC](https://www.ruby-forum.com/t/page-attachments-with-page-type-not-set-to-normal/122255/1 "2007-12-05T23:08:35Z")

</div>

Hi,

I’m using the page\_attachments extension and it is working fine, except  
when I use it on a page that with a page type of anything other than  
normal. I saw an old mailing list post about it, but there was no  
resolution…anyone else having problems with this?

I’m running 0.6.4 Radiant with mongrel and I have some extensions  
installed including default\_page\_parts.

Thanks!

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [December 5, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/page-attachments-with-page-type-not-set-to-normal/122255/2 "2007-12-05T23:58:46Z")

</div>

I think I’ve got part of the solution figured out. After digging into  
the code a little and poking around console, I think I understanding  
what’s going on.

page\_attachments is literally attached to a page with class Page. In  
page\_attachments\_extension.rb, the PageAttachment stuff gets  
class\_eval’ed onto Page.

* * *

## Page.class\_eval { include PageAttachmentAssociations include PageAttachmentTags }

Also, I see in the PageAttachment class definition:

class PageAttachment \< ActiveRecord::Base  
…

belongs\_to :page  
end

From looking at a page with page type “Archive” in console:  
#\<ArchivePage:0x25cdc48 @attributes={“slug”=\>"/",  
“class\_name”=\>“ArchivePage”, …

This is no longer a page with class Page, it is an ArchivePage and  
ArchivePage hasn’t received the PageAttachment extensions.

As an experiment, I tried just duplicating the include code above and  
eval’ing it on ArchivePage:

* * *

## ArchivePage.class\_eval { include PageAttachmentAssociations include PageAttachmentTags }

I restarted my server and tried to edit the page, but got stopped with  
an error:

SQLite3::SQLException: no such column: page\_attachments.archive\_page\_id:  
SELECT count(\*) AS count\_all FROM page\_attachments WHERE  
(page\_attachments.archive\_page\_id = 1)

What I noticed after that was that PageAttachmentAssociations evals the  
following when it gets included into a class:

has\_many :attachments, :class\_name =\> “PageAttachment”, :dependent =\>  
:destroy

Adding :foreign\_key =\> “page\_id” should clear up the error that just  
stopped us:

has\_many :attachments, :class\_name =\> “PageAttachment”, :dependent =\>  
:destroy, :foreign\_key =\> “page\_id”

I restarted my server and tried to attach an image to the Archive page  
and it worked. So now, the main issue is just figuring out a way to  
include PageAttachments gracefully into all of the page types when the  
extension gets activated. Anyone have any ideas?

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [December 6, 2007, 12:16am UTC](https://www.ruby-forum.com/t/page-attachments-with-page-type-not-set-to-normal/122255/3 "2007-12-06T00:16:37Z")

</div>

Dave,

That sounds kind of hack-ish. The better solution is make sure  
page\_attachments loads before anything else. Uncomment/put this line in  
config/environment.rb:

config.extensions = [:page\_attachments, :all]

Although I haven’t heard of changes to a superclass not propagating to  
subclasses like this, it may have to do with associations.

Sean

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [December 6, 2007, 12:21am UTC](https://www.ruby-forum.com/t/page-attachments-with-page-type-not-set-to-normal/122255/4 "2007-12-06T00:21:31Z")

</div>

Sean C. wrote:

> Dave,
> 
> That sounds kind of hack-ish. The better solution is make sure  
> page\_attachments loads before anything else. Uncomment/put this line in  
> config/environment.rb:
> 
> config.extensions = [:page\_attachments, :all]
> 
> Although I haven’t heard of changes to a superclass not propagating to  
> subclasses like this, it may have to do with associations.
> 
> Sean

Nice work, Sean. I undid all of my changes and this solution worked  
perfectly!

Thanks!
