Ruby on rails 作为一棵树,有很多:因为没有很好地合作

Ruby on rails 作为一棵树,有很多:因为没有很好地合作,ruby-on-rails,ruby-on-rails-3,activerecord,has-many-through,acts-as-tree,Ruby On Rails,Ruby On Rails 3,Activerecord,Has Many Through,Acts As Tree,我有以下资料: class Menu < ActiveRecord::Base has_many :menu_headers # has_many :menu_headers, :conditions => {:parent_id => 0} - was trying # to set parent_id to 0 for top level has_many :menu_items, :through => :menu_headers end cl

我有以下资料:

class Menu < ActiveRecord::Base
  has_many :menu_headers
  # has_many :menu_headers, :conditions => {:parent_id => 0} - was trying 
  # to set parent_id to 0 for top level 
  has_many :menu_items, :through => :menu_headers
end

class MenuHeader < ActiveRecord::Base
  belongs_to :menu
  has_many :menu_items
  acts_as_tree
end

class MenuItem < ActiveRecord::Base
  #belongs_to :menu
  has_one :menu, :through => :menu_header
  belongs_to :menu_header 
end
结束编辑#3

问题是我无法执行以下操作以返回所有菜单项:

m=Menu.find(5)
m.menu_items
因为菜单中有很多:通过。这将只获取顶级菜单项,而不获取更深层菜单项。我尝试将menu_id添加到menu_标题中,但这迫使我将其放在commented_out行中,这使我返回到仅获取顶级标题的状态。有没有一种方法可以告诉我菜单标题的所有深层次,这样上面的工作就可以了

有解决这个问题的办法吗?我非常坚持使用acts_as_tree,所以使用像awesome_嵌套_set这样的东西并不太可能

thx

编辑-以下评论中的几个问题;我是在凌晨4点写的

编辑#2
我可以通过以下方式获得所有儿童:

class MenuHeader < ActiveRecord::Base
   ...
   # only gets all children of the current menu_header_id
   def all_children
     all = []
     self.children.each do |menu_header|
       all << menu_header
       root_children = menu_header.all_children.flatten
       all << root_children unless root_children.empty?
     end
     return all.flatten
   end

end
thx你可以试试,在我看来,它比做树更好

class Menu < ActiveRecord::Base
  has_many :menu_headers, :conditions => {:parent_id => 0}
  has_many :menu_sub_headers, :class_name => 'MenuHeader'
  has_many :menu_items, :through => :menu_sub_headers
end

class MenuHeader < ActiveRecord::Base
  belongs_to :menu
  has_many :menu_items
  acts_as_tree
end

class MenuItem < ActiveRecord::Base
  has_one :menu, :through => :menu_header
  belongs_to :menu_header 
end

不完全清楚你想要什么和得到什么。您想要
菜单。查找(5).菜单项
返回第5个菜单项(我希望它会返回),还是要返回第5个菜单项的后代的所有
菜单
对象(我不希望它开箱即用)?前者;Menu.find(5)。对于Menu id=5Hmm,Menu_项应返回所有Menu_项。如果您将
MenuItem
更改为
属于:菜单,:通过=>:菜单标题
?嗯。。。我的错误-我更新了模型中的内容
有一个:通过=>:菜单标题
。我试过
归属于:菜单,:通过=>菜单标题
但没有这样的luckAh,等等
MenuHeader
需要
归属:菜单
我有点被acts\u as\u树卡住了,除非我能找到一种方法快速转换它。让我看一看——anestry会支持我正在尝试的吗?我想我看到的关于祖先的问题是,我是否可以将它与has_many:through结合使用。例如,这看起来不太好,因此,它看起来像是同时使用两种策略-让acts_as_tree做它该做的事情,然后创建第二个关系来处理has_many:through?差不多。
充当树
是相当偶然的;主要的一点是,您有两个
菜单
菜单标题
关系-一个带有一个条件,可以缩小到顶层,而另一个选择所有内容,以便
:通过
工作。这对你有用吗?
menus
+----+-------------+------------------+
| id | location_id | name             |
+----+-------------+------------------+
|  1 |         145 | item  cocktails  |
+----+-------------+------------------+

menu_headers                            
+----+----------------------+-----------+---------+
| id | name                 | parent_id | menu_id |
+----+----------------------+-----------+---------+
|  1 | Wines By The Glass   |         0 |       1 |
|  2 | WHITE WINES          |         1 |    NULL |
|  3 | WHITE WINES child #1 |         2 |    NULL |
|  4 | WHITE WINES child #2 |         2 |    NULL |
|  5 | WHITE WINES child #3 |         2 |    NULL |
|  6 | RED WINES            |         0 |       1 |
+----+----------------------+-----------+---------+

menu_items
+----+----------------------------------------------------+----------------+
| id | header                                             | menu_header_id |
+----+----------------------------------------------------+----------------+
|  1 | SAUVIGNON BLANC item #1                            |              2 |
|  2 | MONTEPULCIANO                                      |              6 |
+----+----------------------------------------------------+----------------+
class Menu < ActiveRecord::Base
  has_many :menu_headers, :conditions => {:parent_id => 0}
  has_many :menu_sub_headers, :class_name => 'MenuHeader'
  has_many :menu_items, :through => :menu_sub_headers
end

class MenuHeader < ActiveRecord::Base
  belongs_to :menu
  has_many :menu_items
  acts_as_tree
end

class MenuItem < ActiveRecord::Base
  has_one :menu, :through => :menu_header
  belongs_to :menu_header 
end
m1=Menu.create(:name => "Dinner Menu test", :location_id => 145)
c1=m1.menu_headers.create(:name => "White Wine")
c2=c1.children.create(:name => "Sauvignon Blanc", :menu => m1)
mi1=c2.menu_items.create(:header => "SB menu item #1")
mi2=c1.menu_items.create(:header => "SB menu item #2")
m_1.menu_items # should return mi1 and mi2
m_1.menu_headers # should return just c1
m_1.menu_sub_headers # should return c1 and c2