Hello Experts, I have a tree of categories in this category object there is a property called type. the root categories have no type but the children have. so how I can get all the roots with filled in children that have category type = ''B'' for instance. I tried to run Category.roots then delete children With a category not equal to ''B'' but this causes a major performance problems. how to call roots with filter children? best regards Ibrahim -- 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.
This is how children is defined[1]: has_many :children, class_name: name, foreign_key: configuration[:foreign_key], order: configuration[:order], dependent: configuration[:dependent], inverse_of: :parent If you do: root.children.where(:type => yourtype) it should just work. https://github.com/amerine/acts_as_tree/blob/master/lib/acts_as_tree.rb#L7 (https://github.com/amerine/acts_as_tree/blob/master/lib/acts_as_tree.rb#L89)8 -- Oscar Del Ben Sent with Sparrow (http://www.sparrowmailapp.com/?sig) On Friday, June 8, 2012 at 7:18 AM, ibrahim hassan wrote:> Hello Experts, > I have a tree of categories in this category object there is a property > called type. > the root categories have no type but the children have. so how I can get > all the roots with filled in children that have category type = ''B'' for > instance. > I tried to run Category.roots then delete children With a category not > equal to ''B'' but this causes a major performance problems. > > how to call roots with filter children? > > > best regards > Ibrahim > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 8 June 2012 15:18, ibrahim hassan <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello Experts, > I have a tree of categories in this category object there is a property > called type. > the root categories have no type but the children have. so how I can get > all the roots with filled in children that have category type = ''B'' for > instance. > I tried to run Category.roots then delete children With a category not > equal to ''B'' but this causes a major performance problems. > > how to call roots with filter children?Not answering the question but I would avoid using a field named ''type''. Rails expect this to be used with STI and may become confused if it used as a normal field. Colin -- 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.
Thanks for all your answers i will test this and give you a feedback can i use a scope for this also? -- 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.
Hello, It didn''t work i want to bring all roots that a children have one type not only one root. something like @categories = Category.roots but with children condition category_type => ''2'' Can you please help me with this. Best regards -- 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 Jun 11, 3:18 am, ibrahim hassan <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello, > It didn''t work i want to bring all roots that a children have one type > not only one root. > something like > @categories = Category.roots > but with children condition category_type => ''2'' > > Can you please help me with this. > > Best regards > > -- > Posted viahttp://www.ruby-forum.com/.I had a similar problem and this was my solution: Migrate to the awesome_nested_sets gem. You can do this by installing the gem, adding a couple fields to your table, and then running the rebuild command. See the instructions here -> https://github.com/collectiveidea/awesome_nested_set/wiki/Converting-Acts_As_Tree-to-Awesome_Nested_Set I call my nested_set ''Library'' and only wanted users to see the libraries that were either created for their company or not assigned to any company. I created this method in the user table: def libraries libraries = Library.arel_table Library.where(libraries[:company_id].eq(nil).or(libraries[:company_id].eq(self.company_id))) end -- 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.
On Jun 11, 3:18 am, ibrahim hassan <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello, > It didn''t work i want to bring all roots that a children have one type > not only one root. > something like > @categories = Category.roots > but with children condition category_type => ''2'' > > Can you please help me with this. > > Best regards > > -- > Posted viahttp://www.ruby-forum.com/.Sorry if this is a double post but I had browser issues. I had a similar problem and this is how I solved it. First, switch to the awesome_nested_sets gem. After installing the gem, you will need to add a couple fields and run a rebuild command. See the instructions here -> https://github.com/collectiveidea/awesome_nested_set/wiki/Converting-Acts_As_Tree-to-Awesome_Nested_Set My nested set is called ''Library'' and I only wanted users to see libraries that were either created for their company or not assigned to any company (ie. library.company_id == user.company_id or library.company_id == nil). I created this user method: def libraries libraries = Library.arel_table Library.where(libraries[:company_id].eq(nil).or(libraries[:company_id].eq(self.company_id))) end And call it from the controller using: @libraries = current_user.libraries.order(''lft ASC'') In the view, I create a table and then use the jquery.treeTable.js to created a widget that allows for easy navigation of the tree/sets. -- 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.
Hello, thanks for your advice, however i wanted all the roots chould parent should be always nil, but in the same time I want the children for this roots to be filtered only with a specific type. root1- child1 type A child2 type B root2 child3 type A Child4 type B when i apply the filter on type A the result should be as follows: root1 child1 root2 child3 Can someone help me with this? best regards -- 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.