ActiveRecord relationships

Hi, I’m building my own homepage with this wonderfull “new” framework,
so far all has been very smooth until now.

I have a table (MySQL MyISAM) called “pages”, which hold an id, title
and some content, now, I’m trying to make a “page” able to hold another
page by giving each “page”-row a “page_id” column, this clearly wont
work. My experience with table relations is quite limited. Any ideas
how I would solve this as simple and cleanly as possible?

I tried this in the model:

class Pages < ActiveRecord::Base
has_many :pages
end

But when using the above and trying to do:

page.pages.title
in the view I get:
“…/active_support/dependencies.rb:100:in `const_missing’: uninitialized constant Page”

Using page.page just gives me an “undefined method” error.

Thanks in advance

Can you explain your model a little differently maybe? I’m not sure I
understand how you have a page within a page. I think you might need to
redesign so that you have another object that can contain pages, like a
chapter. I’ve not dealt much with tables that refer to themselves.
Usually
you’ll have table A that will contain table B’s ID. So, using the
example
of Chapters, you could have a Chapter table that has a Page_Id and then
use
a has_a relationship in the model. I’m not sure what the relationship
would
be for a self-relationship…

Terry

On 9/11/06, [email protected] [email protected] wrote:

Using page.page just gives me an “undefined method” error.

Thanks in advance


Terry (TAD) Donaghe
http://www.tadspot.com

Hmm yeah, Its looking like that is what I’ll have to do… My idea was
to have a self-relationship easily allowing infinite levels of child
objects or something like that… but I can’t figure out a way to do
that.

Has anyone else got any clues?

On Sep 11, 2006, at 4:57 PM, [email protected] wrote:

Hmm yeah, Its looking like that is what I’ll have to do… My idea was
to have a self-relationship easily allowing infinite levels of child
objects or something like that… but I can’t figure out a way to do
that.

Has anyone else got any clues?

Sounds like you want to use  acts_as_tree.

http://ar.rubyonrails.com/classes/ActiveRecord/Acts/Tree/
ClassMethods.html

-Ezra

Ezra Z. wrote:

Sounds like you want to use acts_as_tree.

Peak Obsession
ClassMethods.html

-Ezra

Thank you guys, used “acts_as_tree” and it works splendid. Might as
well whip up a How To on rubyonrails.org :slight_smile: