skurmedel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Sep-11 21:39 UTC
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 > endBut when using the above and trying to do:> page.pages.titlein 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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, skurmedel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <skurmedel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > 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 > > > > >-- Terry (TAD) Donaghe http://www.tadspot.com http://www.rubynoob.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
skurmedel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Sep-11 23:57 UTC
Re: ActiveRecord relationships..
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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On Sep 11, 2006, at 4:57 PM, skurmedel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
skurmedel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Sep-12 10:32 UTC
Re: ActiveRecord relationships..
Ezra Zygmuntowicz wrote:> On Sep 11, 2006, at 4:57 PM, skurmedel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org 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 > > -EzraThank you guys, used "acts_as_tree" and it works splendid. Might as well whip up a How To on rubyonrails.org :) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---