Good morning, is it possible to find objects by giving values for the HABTM join table only? Example: menu id name parents path 1 abc -1 / 2 def 1 /def 3 ghi 1 /ghi 4 jkl 2,1 /jkl 5 mno 2 /mno that means when I go to /abc the menu consists of menu items with the id 2, 3 and 4. When I go to /def the menu consits of the menu items with the id 4 and 5 but as you can see menu items can be shown in many menus but exist only once. So the tables would look like this: menus id name path 1 abc / 2 def /def 3 ghi /ghi 4 jkl /jkl 5 mno /mno menus_parents menu_id parent_id 2 1 3 1 4 1 4 2 5 2 So how is it possible to all menu objects that have 1 as parent? My current approach is: menus = Menu.all menus = menus.select{|m| m.parents.include?(1)} I don''t really like it cause all menu items are loaded from the database each and every time. Is there any better solution to this like some find...? -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 15 July 2010 09:30, Heinz Strunk <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Good morning, > > is it possible to find objects by giving values for the HABTM join table > only? > > Example: > menu > id name parents path > 1 abc -1 / > 2 def 1 /def > 3 ghi 1 /ghi > 4 jkl 2,1 /jkl > 5 mno 2 /mno > > that means when I go to /abc the menu consists of menu items with the id > 2, 3 and 4. When I go to /def the menu consits of the menu items with > the id 4 and 5 but as you can see menu items can be shown in many menus > but exist only once. > > So the tables would look like this: > menus > id name path > 1 abc / > 2 def /def > 3 ghi /ghi > 4 jkl /jkl > 5 mno /mno > > menus_parents > menu_id parent_id > 2 1 > 3 1 > 4 1 > 4 2 > 5 2 > > So how is it possible to all menu objects that have 1 as parent? > My current approach is: > menus = Menu.all > menus = menus.select{|m| m.parents.include?(1)}Can''t you do menus = Menu.find( :all, :include => :parents, :conditions => [''parent.id = ?, 1] ) or something similar. Or even better, if you have the parent object, then all the menus that have that as a parent are parent.menus Colin> > I don''t really like it cause all menu items are loaded from the database > each and every time. Is there any better solution to this like some > find...? > -- > Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hey Colin, thanks but the parent.menus doesn''t work because the habtm is on the same table: has_and_belongs_to_many :parents, :class_name => "Menu", :association_foreign_key => "parent_id", :join_table => "menus_parents" The other suggestion works just fine, thanks! This is the working code: @menus = Menu.find(:all, :include => :parents, :conditions => [''`menus_parents`.parent_id = ?'', parent.id]) -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 15 July 2010 14:22, Heinz Strunk <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hey Colin, > > thanks but the parent.menus doesn''t work because the habtm is on the > same table: > has_and_belongs_to_many :parents, :class_name => "Menu", > :association_foreign_key => "parent_id", :join_table => "menus_parents"I think there may be a better way of doing this. If you made the join table a proper table with a model then for Menu you could do has_many children through menus_parents and also has_many parents through menus_parents then you can go both ways more easily. Colin> > The other suggestion works just fine, thanks! > > This is the working code: > @menus = Menu.find(:all, :include => :parents, :conditions => > [''`menus_parents`.parent_id = ?'', parent.id])-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.