Josh,
We're doing something similar, except that instead of using childable
and parentable, we're using awesome_nested_set.  It enables you to
have sub_categories within sub_categories, etc., and its interface is
pretty intuitive and efficient:

http://wiki.github.com/collectiveidea/awesome_nested_set/awesome-nested-set-cheat-sheet


Tilde Equals


On May 25, 12:29 pm, Josh Cheek <[email protected]> wrote:
> Hi, seem to keep running into a wall here. I can't find any resources on
> site navigation that can deal with any model being in the nav, allow
> nesting, and can dynamically update.
>
> So I thought about it for a while, and decided on a MenuItems class, which
> contained the position of the child in relation to it's siblings, where the
> parent and the child were polymorphic. Then a given childable object can
> find it's parentable by going through the menu_items table.
>
> I want my nav to be able to do things like this:
> --Category1
> ----SubCategory1
> ------Product1
> ------Product2
> ----Product3
> --Category2
> ----Product4
> --Page1
> --Page2
> --Page3
>
> This is the current setup:
>
> MODELS
>
> class MenuItem < ActiveRecord::Base
>   belongs_to :childable  , :polymorphic => true
>   belongs_to :parentable , :polymorphic => true
>
>   acts_as_list :scope => :parentable_id
> end
>
> class Category < ActiveRecord::Base
>   has_one  :parent_menu_item , :as => :parentable , :class_name =>
> 'MenuItem'
>   has_many :child_menu_items , :as => :childable  , :class_name =>
> 'MenuItem'
>
>   has_one  :parentable   , :through => :parent_menu_item
>   has_many :childables   , :through => :child_menu_items
> end
>
> class SubCategory < ActiveRecord::Base
>   has_many :child_menu_items , :as => :childable  , :class_name =>
> 'MenuItem'
>   has_one  :parent_menu_item , :as => :parentable , :class_name =>
> 'MenuItem'
>
>   has_one  :parent   , :through => :parent_menu_item
>   has_many :children , :through => :child_menu_items
> end
>
> class Page < ActiveRecord::Base
>   has_one  :parent_menu_item , :as => :parentable , :class_name =>
> 'MenuItem'
>   has_one  :parent   , :through => :parent_menu_item
> end
>
> SCHEMA:
> ActiveRecord::Schema.define(:version => 20100525184637) do
>
>   create_table "categories", :force => true do |t|
>     t.datetime "created_at", :null => false
>     t.datetime "updated_at", :null => false
>   end
>
>   create_table "menu_items", :force => true do |t|
>     t.integer  "position",        :null => false
>     t.integer  "parentable_id",   :null => false
>     t.string   "parentable_type", :null => false
>     t.integer  "childable_id",    :null => false
>     t.string   "childable_class", :null => false
>     t.datetime "created_at",      :null => false
>     t.datetime "updated_at",      :null => false
>   end
>
>   create_table "pages", :force => true do |t|
>     t.datetime "created_at", :null => false
>     t.datetime "updated_at", :null => false
>   end
>
>   create_table "sub_categories", :force => true do |t|
>     t.datetime "created_at", :null => false
>     t.datetime "updated_at", :null => false
>   end
>
> end
>
> I have had a lot of trouble with it, this is the best I've gotten so far,
> but I am still getting the error:
> ActiveRecord::HasManyThroughAssociationPolymorphicError: Cannot have a
> has_many :through association 'Category#childables' on the polymorphic
> object 'Childable#childable'.
>
> Is there a way to make this work? Is there a better way to do this?

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to