# Adding methods to Page from an extension

**URL:** <https://www.ruby-forum.com/t/adding-methods-to-page-from-an-extension/84392>\
**Category:** Radiant CMS\
**Created:** [February 4, 2007, 12:10am UTC](https://www.ruby-forum.com/t/adding-methods-to-page-from-an-extension/84392 "2007-02-04T00:10:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![igotimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@igotimac](https://www.ruby-forum.com/u/igotimac)\
**Post date:** [February 4, 2007, 12:10am UTC](https://www.ruby-forum.com/t/adding-methods-to-page-from-an-extension/84392/1 "2007-02-04T00:10:05Z")

</div>

Hey I feel like I’m missing something obvious,

I created an extension that adds some columns to the pages table, and  
now I want to add some methods to the Page class.

I put this at the bottom of my Extension class

class Page \< ActiveRecord::Base

def test\_new\_method  
“hi”  
end

end

And now I’m getting errors like:

undefined method `children' for #<Page:0x2280638> undefined method`parts’ for #Page:0x2280638

Which leads me to believe that my version of Page is being loaded, and  
the original version is not.

How do I get them both to load? what’s really going on here?

Also, can somebody tell why “logger” is not defined from my Extension  
class  
How can I access the logger?

thanks,  
Jacob

---

<div class="post-metadata">

**Author:** ![igotimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@igotimac](https://www.ruby-forum.com/u/igotimac)\
**Post date:** [February 4, 2007, 12:17am UTC](https://www.ruby-forum.com/t/adding-methods-to-page-from-an-extension/84392/2 "2007-02-04T00:17:26Z")

</div>

> class Page \< ActiveRecord::Base

Should be:

YourPageClassName \< Page

---

<div class="post-metadata">

**Author:** ![igotimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@igotimac](https://www.ruby-forum.com/u/igotimac)\
**Post date:** [February 4, 2007, 2:34am UTC](https://www.ruby-forum.com/t/adding-methods-to-page-from-an-extension/84392/3 "2007-02-04T02:34:11Z")

</div>

Loren J. wrote:

> > class Page \< ActiveRecord::Base
> 
> Should be:
> 
> YourPageClassName \< Page

No, I don’t want to subclass page, I want to add methods to all pages.

I could directly edit page.rb but I’d like to limit my edits to remain  
within the folder for my extension.

---

<div class="post-metadata">

**Author:** ![igotimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@igotimac](https://www.ruby-forum.com/u/igotimac)\
**Post date:** [February 4, 2007, 3:11am UTC](https://www.ruby-forum.com/t/adding-methods-to-page-from-an-extension/84392/4 "2007-02-04T03:11:08Z")

</div>

Jacob B. wrote:

> Loren J. wrote:
> 
> > > class Page \< ActiveRecord::Base
> > 
> > Should be:
> > 
> > YourPageClassName \< Page
> 
> No, I don’t want to subclass page, I want to add methods to all pages.
> 
> I could directly edit page.rb but I’d like to limit my edits to remain  
> within the folder for my extension.

I am redefining page in the app/models directory of my extension

and in my extension definition I can explicitly load this verison of  
page

like this:

load  
“#{RADIANT\_ROOT}/vendor/extensions/page\_attributes/app/models/page.rb”

UGLY!!!

but that works…  
Shouldn’t it just get loaded by default. What do I do to fix this?

---

<div class="post-metadata">

**Author:** ![igotimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@igotimac](https://www.ruby-forum.com/u/igotimac)\
**Post date:** [February 4, 2007, 3:17am UTC](https://www.ruby-forum.com/t/adding-methods-to-page-from-an-extension/84392/5 "2007-02-04T03:17:41Z")

</div>

It might be better to put your added methods in a module and include  
them in Page inside the activate method of your extension, like so:

module NewPageMethods  
def foo  
“bar”  
end  
end

# in your extension

def activate  
Page.send :include, NewPageMethods  
# …  
end

Sean

---

<div class="post-metadata">

**Author:** ![igotimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@igotimac](https://www.ruby-forum.com/u/igotimac)\
**Post date:** [February 4, 2007, 3:24am UTC](https://www.ruby-forum.com/t/adding-methods-to-page-from-an-extension/84392/6 "2007-02-04T03:24:41Z")

</div>

Jacob B. wrote:

> Shouldn’t it just get loaded by default. What do I do to fix this?

Put your methods in a module and mix them into page in your extension’s  
activate method.

–  
John L.  
[http://wiseheartdesign.com](http://wiseheartdesign.com)

---

<div class="post-metadata">

**Author:** ![igotimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@igotimac](https://www.ruby-forum.com/u/igotimac)\
**Post date:** [February 4, 2007, 3:53am UTC](https://www.ruby-forum.com/t/adding-methods-to-page-from-an-extension/84392/7 "2007-02-04T03:53:38Z")

</div>

> ```
> Page.send :include, NewPageMethods
> 
> ```

That’s the magic I was looking for thanks.
