# Extension advice

**URL:** <https://www.ruby-forum.com/t/extension-advice/122655>\
**Category:** Radiant CMS\
**Created:** [December 10, 2007, 12:42pm UTC](https://www.ruby-forum.com/t/extension-advice/122655 "2007-12-10T12:42:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 10, 2007, 12:42pm UTC](https://www.ruby-forum.com/t/extension-advice/122655/1 "2007-12-10T12:42:48Z")

</div>

Hello again,

I’m attempting to write a couple of extensions, and have what is  
hopefully a quick question.

I have an extension that creates a table for age and extends pages  
with two columns age\_min, and age\_max. From the edit page I can post  
the id’s of age into age\_min and age\_max, and from the actual page can  
return the values of those id’s. However, I would like to return the  
name value connected to those id’s in age and not the actual id itself.

What would be a good example to look at, in order to achieve the above?

Many thanks,

Dominic

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 12, 2007, 6:47pm UTC](https://www.ruby-forum.com/t/extension-advice/122655/2 "2007-12-12T18:47:27Z")

</div>

I’m not quite sure what you’re trying to do. My recommendation is to  
check [http://wiki.radiantcms.org/Thirdparty\_Extensions](http://wiki.radiantcms.org/Thirdparty_Extensions) for existing  
extensions. Even if they don’t have what you want, there’s usually  
some good examples.

Incidentally, are you trying to do something like delayed publishing,  
where a page has to be a certain age before it’s visible, and it  
expires after a certain age? Or am I completely misunderstanding your  
age\_min and age\_max examples… If that’s what you’re trying to do,  
check out the Scheduler extension.

-Andrew

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 12, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/extension-advice/122655/3 "2007-12-12T18:57:40Z")

</div>

Hi Andrew,

Thank you for replying, I’ve been looking through the extensions as  
you say, but can’t really see anything close enough to what I want to  
do in this particular case (from my limited knowledge). I think one of  
the closest is perhaps Author, where the ID of the author name gets  
entered in to the page table under the created\_by column and the  
Author Name gets returned from the ID on the pages table by it tag.  
Its the connection between the local id and the foreign name that I  
think I need to look up.

I’m a bit code-blind after all the code I’ve been looking at and  
learning over the past few day’s, however I now think I have to  
investigate associations and belongs\_to in order to do the above for  
my extension. If I’m heading down the wrong path someone please tell  
me! Hopefully I’ll have a fresh look at it tomorrow, I’m hoping its  
just a simple concept I have yet to digest…

I’m voluntarily building a site for a local theatre company as a  
project to learn ruby/rails/radiant, so I’m learning as I go (in at  
the deep end), the min\_age and max\_age aren’t really connected to the  
other date extension I am doing. This one is just going to be a used  
as an aid to find pages for particular age groups (and to show those  
values on the particular events). I don’t think having two values will  
prove to be the final/best solution, as a single multiple select  
option box may be a better method to achieve the same ends, but this  
part is working passably for now.

The previous example which you so kindly helped with was based on  
Sean’s great Schedular extension. It adds start and end dates for  
event pages (which are slightly different from publish dates in my  
case), it also enables those values to be displayed on the page and to  
order by them. As the information is stored directly on the page table  
it is relatively easy for me to access it directly, the trouble I am  
currently facing is matching a ID of one thing to a value in a  
different DB table.

If anyone can point a good direction to look at for research that  
would be great.

Thanks once again

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 13, 2007, 2:58am UTC](https://www.ruby-forum.com/t/extension-advice/122655/4 "2007-12-13T02:58:19Z")

</div>

Thank you Andrew for all this,

After hours of struggling to grasp this core concept I think finally  
have it and your post arrived at just the time to reassure me I  
finally got it right - the joys of learning! - it’s all looking a lot  
simpler now! I think I got hung up on belongs\_to being and has\_many  
being the wrong way around in my head and went in every wrong  
direction from there.

I think my next goal is to use a single multiple select box in the  
admin area to try and get the values returned as the selected options,  
which I think might need a different type of association.

But, yes, once the bug’s has really bitten, I should imagine  
developing it in a way very similar to what you describe - perhaps  
even widening it to a more general and very simple page tagging  
engine. (maybe)

Many many thanks for your kind help,

Dominic

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 12, 2007, 7:15pm UTC](https://www.ruby-forum.com/t/extension-advice/122655/5 "2007-12-12T19:15:14Z")

</div>

Hi Dominic,

Ah – everything is much more clear now. What you’re describing is a  
fairly core behavior of Rails. Associations are indeed the right way  
to go here.

For example, say you have an extension called Ages, which you would  
create with “script/generate extension Ages”. In the extension, you  
have a model, AgeGroup (created with “script/generate extension\_model  
Ages AgeGroup”).

The create\_table part of the migration for AgeGroup might look  
something like (note that none of these examples have been tried yet,  
so I may have minor syntax errors):

create\_table do |t|  
t.column :min, :integer  
t.column :max, :integer  
end

and the class might look like:

class AgeGroup \< ActiveRecord::Base  
has\_many :pages  
end

and somewhere else (either /app/models or /lib) have something like:

module PageExtension  
belongs\_to :age\_group  
end  
Page.send(:include, AgePageExtension)

You should explicitly require this file in your age\_extension.rb.

Of course, you’ll need a migration to alter pages to have an  
:age\_group\_id column (and you’ll probably want an index on that too).

You’ll also need a controller for Admin-ing the age\_groups table. It  
should be simple CRUD operations, which other extensions cover pretty  
well.

After you understand that, it’s time to move on to something a little  
more complicated: To actually display the information, my  
recommendation is to do something similar to the ArchivePages. Have  
one page that lists all of the age groups with links to a child page  
for each age group. The child pages would actually be a single  
virtual page that gets the age-group specified in the URL. Although  
code-blindness will hurt here, I recommend you look at  
radiant/app/models/archive\_page.rb,  
radiant/app/models/archive\_\*\_index\_page.rb and  
radiant/lib/archive\_index\_tags\_and\_methods.rb for an idea of what I’m  
talking about.

The most important things are that ArchivePage overrides child\_url  
(the method responsible for making a url for a page’s child) and  
find\_by\_url (the method responsible for returning a Page to render).  
Also, tags are a good thing for dynamic content (such as a list of  
pages for a certain age group).

Hope this helps,  
Andrew
