# Extending Models in Extensions

**URL:** <https://www.ruby-forum.com/t/extending-models-in-extensions/90418>\
**Category:** Radiant CMS\
**Created:** [March 28, 2007, 4:53pm UTC](https://www.ruby-forum.com/t/extending-models-in-extensions/90418 "2007-03-28T16:53:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rpheath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@rpheath](https://www.ruby-forum.com/u/rpheath)\
**Post date:** [March 28, 2007, 4:53pm UTC](https://www.ruby-forum.com/t/extending-models-in-extensions/90418/1 "2007-03-28T16:53:09Z")

</div>

Hi Folks… Longtime Radiant user, just getting started with Mental  
Extensions.

I am creating an extension for posting and managing Comments. I’ve  
created the Comment model, my controllers, and views in the new  
Extension, but I’m having trouble extending Radiant’s built-in Page  
model to include the proper has\_many association.

In a nutshell: I want to include a has\_many association in the Page  
model.

It seems like I should be able to just open the Page class and  
include my association like this:

class Page \< ActiveRecord::Base  
has\_many :comments  
end

in …/extensions/comments/app/models/page.rb

This doesn’t work, of course. It doesn’t return an error - as far as  
I can tell it just doesn’t do anything. Referencing @page.comments in  
my views throws an error: “undefined method `comments’”. I presume  
it’s because it is ignoring this class.

I’ve also tried several module mix-ins that haven’t worked (i.e.  
packaging a PageExtender module and then including it in Page). For  
example:

module PageExtender  
def self.included(base)  
base.extend(ClassMethods)  
end  
module ClassMethods  
has\_many :comments  
end  
end

in …/extensions/comments/lib/page\_extender.rb

And then

def activate  
…  
Page.send :include, PageExtender  
end

in …/extensions/comments/comments\_extension.rb

But this terminates with  
~/Sites/radiant.mental/vendor/extensions/comments/lib/  
page\_extender.rb:8: undefined method `has\_many’ for  
PageExtender::ClassMethods:Module (NoMethodError)

Any ideas?

Thanks,  
Ryan H.

---

<div class="post-metadata">

**Author:** ![rpheath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@rpheath](https://www.ruby-forum.com/u/rpheath)\
**Post date:** [March 28, 2007, 4:58pm UTC](https://www.ruby-forum.com/t/extending-models-in-extensions/90418/2 "2007-03-28T16:58:10Z")

</div>

I think I just solved my own problem.

Evidently it is easy to open up and extend the Page model

I put:

> class Page \< ActiveRecord::Base  
> has\_many :comments  
> end
> 
> in …/extensions/comments/app/models/page.rb

and then

require File.dirname( **FILE** ) + ‘/app/models/page’

in the extension definition.

On Mar 28, 2007, at 9:51 AM, Ryan H. wrote:

> I can tell it just doesn’t do anything. Referencing @page.comments in  
> end  
> …  
> Any ideas?
> 
> Thanks,  
> Ryan H.
> 
> * * *
> 
> Radiant mailing list  
> Post: removed\_email\_address@domain.invalid  
> Search: [http://radiantcms.org/mailing-list/search/](http://radiantcms.org/mailing-list/search/)  
> Site: [http://lists.radiantcms.org/mailman/listinfo/radiant](http://lists.radiantcms.org/mailman/listinfo/radiant)

* * *

Ryan H.  
Art of Mission, Inc.  
3720 Gattis School Rd #800 PMB 245  
Round Rock, TX 78664

800-722-1492 (phone)

[www.artofmission.com](http://www.artofmission.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![rpheath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@rpheath](https://www.ruby-forum.com/u/rpheath)\
**Post date:** [March 28, 2007, 5:06pm UTC](https://www.ruby-forum.com/t/extending-models-in-extensions/90418/3 "2007-03-28T17:06:15Z")

</div>

Ryan H. wrote:

> ```
> end
> 
> ```
> 
> …
> 
> Any ideas?

Try:

def activate  
Page.class\_eval do  
has\_many :comments  
end  
end

Or even:

```
module CommentsPageExtender
  def self.included(base)
    base.class_eval do
      has_many :comments
    end
  end
end

```

And:

def activate  
…  
Page.send :include, CommentsPageExtender  
end

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

---

<div class="post-metadata">

**Author:** ![rpheath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@rpheath](https://www.ruby-forum.com/u/rpheath)\
**Post date:** [March 29, 2007, 5:26pm UTC](https://www.ruby-forum.com/t/extending-models-in-extensions/90418/4 "2007-03-29T17:26:20Z")

</div>

I have been told that “send” calls will be deprecated in future  
versions of Ruby and one should use class\_eval or module\_eval. Just  
FYI. class\_eval would work for most of these cases.

Sean

---

<div class="post-metadata">

**Author:** ![rpheath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@rpheath](https://www.ruby-forum.com/u/rpheath)\
**Post date:** [March 29, 2007, 8:32am UTC](https://www.ruby-forum.com/t/extending-models-in-extensions/90418/5 "2007-03-29T08:32:25Z")

</div>

> > def activate  
> > …  
> > Page.send :include, PageExtender  
> > end

Page.send(“has\_many”, “comments”.intern)

## would also work.

Sander de Goeij  
removed\_email\_address@domain.invalid
