hello, this is probably a simple problem but I havn''t found the elegant/simple RoR way to solve it. I have a Parent and a Child table with a has_many :children and a belongs _to :parent, relationship. I view all childs through each parent this way: <% for parent in Parent.find_all %> parent.name parent.other_attribute for child in parent.children child.name child.age child activity etc end <% end %> It works fine. Now I want the same thing but with a condition in the child table. For example only the children below a certain age (...WHERE children.age < 12 ) also if the parent hasn''t got any children below 12 it musn''t appear. If the condition lay in the parent it''s simple instead of : Parent.find_all i put : Parent.find(:all, :conditions => "profession = ''doctor''"). But this Parent.find(:all, :conditions => "child.age < 12") does not. My best guess is there would be a method in the model class Child < ActiveRecord or maybe something with :join ... thank you for your help charly _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Charly wrote:> hello, this is probably a simple problem but I havn''t found the > elegant/simple RoR way to solve it. I have a Parent and a Child table > with a has_many :children and a belongs _to :parent, relationship. I > view all childs through each parent this way: <% for parent in > Parent.find_all %> parent.name parent.other_attribute for child in > parent.children child.name child.age child activity etc end <% end %> > > > It works fine. Now I want the same thing but with a condition in the > child table. For example only the children below a certain age > (...WHERE children.age < 12 ) also if the parent hasn''t got any > children below 12 it musn''t appear. > > If the condition lay in the parent it''s simple instead of : > Parent.find_all i put : Parent.find(:all, :conditions => "profession > = ''doctor''"). But this Parent.find(:all, :conditions => "child.age < > 12") does not.:conditions is just a lump of SQL, so unless you''ve got the child table joined to the parent table in the Parent.find() query, it won''t have anything to do the comparison with. The following *might* work, but I haven''t tested it: Parent.find(:all, :include=>[''child''], :conditions=>''child.age < 12'') That should make the join to the child table work, and you won''t get parents without young children. By the way, do you have a good reason for storing ages as years? It''s usually a better idea to store dates of birth - that way you don''t have to update all your rows every year :-) -- Alex
Thanks, it worked perfectly, beyond my expectations - really pleased with rails. If I understand well the :include argument force RoR to do the associations which otherwise are only made at runtime (for ex: when the iteration gets to child.name). Therefore it makes all associated attribute available for processing in the find() function. By the way my parent child table were just there for illustration. The real ones are Lawyer, Matter and Fee. The fees have timestamps and I wanted only the fees of a given year (no ages stored here) Charly "Alex Young" <alex-qV/boFbD8Meu8LGVeLuP/g@public.gmane.org> a écrit dans le message de news: 43327F10.2030801-qV/boFbD8MeyI+KclE3oSK1cXZ9k6wlg@public.gmane.org> Charly wrote: >> hello, this is probably a simple problem but I havn''t found the >> elegant/simple RoR way to solve it. I have a Parent and a Child table >> with a has_many :children and a belongs _to :parent, relationship. I >> view all childs through each parent this way: <% for parent in >> Parent.find_all %> parent.name parent.other_attribute for child in >> parent.children child.name child.age child activity etc end <% end %> >> >> >> It works fine. Now I want the same thing but with a condition in the >> child table. For example only the children below a certain age >> (...WHERE children.age < 12 ) also if the parent hasn''t got any >> children below 12 it musn''t appear. >> >> If the condition lay in the parent it''s simple instead of : >> Parent.find_all i put : Parent.find(:all, :conditions => "profession >> = ''doctor''"). But this Parent.find(:all, :conditions => "child.age < >> 12") does not. > :conditions is just a lump of SQL, so unless you''ve got the child table > joined to the parent table in the Parent.find() query, it won''t have > anything to do the comparison with. The following *might* work, but I > haven''t tested it: > > Parent.find(:all, :include=>[''child''], :conditions=>''child.age < 12'') > > That should make the join to the child table work, and you won''t get > parents without young children. > > By the way, do you have a good reason for storing ages as years? It''s > usually a better idea to store dates of birth - that way you don''t have > to update all your rows every year :-) > > -- > Alex
Alex Young
2005-Sep-23 08:46 UTC
Re: Re: Filtering with conditions in the child table/class
Charly wrote:> If I understand well the :include argument force RoR to do the associations > which otherwise are only made at runtime (for ex: when the iteration gets to > child.name). Therefore it makes all associated attribute available for > processing in the find() function.That''s it - it forces greedy evaluation. The availability to the :conditions clause is a side-effect - but an extremely useful one :-)> By the way my parent child table were just there for illustration. The real > ones are Lawyer, Matter and Fee. The fees have timestamps and I wanted only > the fees of a given year (no ages stored here)Ah... All is clear. -- Alex