so i have a parent and child model, i need to get all the entries in my parent model that don''t yet have a child. i can do a find all and iterate through all the buggers, but this is a little cpu intensive. How can i get something like this pseudo code to work?? @parents = Parent.find(:all, :conditions => [ :children => nil ]) for bonus points you can do the same thing with a parent, child , and child''s child. @parents = Parent.find(:all, :include => :children, :conditions => [:childrens-children => nil ]) Thanks, Richard http://www.slangslang.com -- 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 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?hl=en -~----------~----~----~----~------~----~------~--~---
some code would be nice. Especially if you give bonus points! if the parent belongs_to child or viceversa, then you can use your format above. However, if you have a join table (HABTM or HMT) then you will need to do a find based on that join table JoinTable.find(:all, :conditions => {:child_id => nil }) j On Jul 9, 3:40 am, Richard Schneeman <rails-mailing-l...@andreas- s.net> wrote:> so i have a parent and child model, i need to get all the entries in my > parent model that don''t yet have a child. i can do a find all and > iterate through all the buggers, but this is a little cpu intensive. How > can i get something like this pseudo code to work?? > > @parents = Parent.find(:all, :conditions => [ :children => nil ]) > > for bonus points you can do the same thing with a parent, child , and > child''s child. > > @parents = Parent.find(:all, :include => :children, :conditions => > [:childrens-children => nil ]) > > Thanks, > Richardhttp://www.slangslang.com > -- > Posted viahttp://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 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 could use counter_cache in the association, though this will require you to add a children_count to your parents table. http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html Alternatively, you could use a bit of sql: @childless_parents = Parent.find_by_sql("select * from parents where id not in (select distinct parent_id from parents") This assumes that parents and children are actually seperate classes: if they''re the same class, and they''re all in the ''parents'' table then it''s @childless_parents = Parent.find_by_sql("select * from parents where id not in (select distinct parent_id from parents") Which is like saying ''give me all the parents who aren''t a parent to a child". The second approach can be extended to grandchildren as well, by finding all the grandparents and subtracting them from the total list of parents. If parents, children, and grandchildren are all the same class of object then you should have a look at the acts_as_tree plugin, which is useful for this sort of thing. -- 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 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?hl=en -~----------~----~----~----~------~----~------~--~---
> @childless_parents = Parent.find_by_sql("select * from parents where id > not in (select distinct parent_id from parents")oops, just noticed that the inner nested query is missing a close bracket. -- 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 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?hl=en -~----------~----~----~----~------~----~------~--~---
At the expense of some CPU: Model.find(:all).select { |m| !m.children.blank? } Rather than checking to see if they''re nil, because [] != nil, use .blank? which checks for nil and emptiness. For the children children... Model.find(:all).select { |m| ! m.children.map(&:children).flatten.empty? } \ --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
Parent.find :all, :include => :children, :conditions => "child_table.id IS NULL" should work, since :include generates left outer joins. Fred --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
At the expense of some CPU: Model.find(:all).select { |m| !m.children.blank? } Rather than checking to see if they''re nil, because [] != nil, use .blank? which checks for nil and emptiness. For the children children... Model.find(:all).select { |m| ! m.children.map(&:children).flatten.empty? } --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---