On 2 Jan 2008, at 10:48, Kei Simone wrote:
>
> To all:
>
> hi,
>
> i have implemented the associations in my models
>
> so i have a Parent model that has many Children model
>
> but i need to get a list of Parents that do not have children
> or
> Parents that have just 2 children
>
> or Parents that have children whose name is ''James'', or
''Sarah'', or
> ''Betty''
>
> or Parents whose children do not have names like ''James'',
or ''Sarah'',
> or ''Betty''
>
> I find it near impossible to use activerecord and the various find
> methods.
You''re right that activerecord won''t help you much with this
sort of
stuff - you will be writing out a bunch of joins yourself (with the
exception of ''parents with 2 children'': you can use a
counter_cache,
at which point it becomes trivial).
To get all parents with no children, you need to be generating sql of
the form
select parents.* from parents
left outer join children on children.parent_id = parents.id
where children.id IS NULL
if you want parents whose children match some condition
select parents.* from parents
inner join children on children.parent_id = parents.id
where <some conditions on children>
if you want parents with no children matching some condition
select parents.* from parents
left outer join children on children.parent_id = parents.id AND <some
conditions on children>
where children.id IS NULL
You can tell activerecord what joins to apply with the :joins option
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
-~----------~----~----~----~------~----~------~--~---