Hello all,
I am a newbie and therefore I am currently discovering the delights of
Activerecord. I have a collection of objects, some of which have
children and some of which have not. I am trying to run a find which
will retrieve all parent objects which have children and then return
an array. I know there is some sleek way to achieve this, and not the
cumbersome iterative approach which looks to be looming ahead.
Any help is mucho appreciated...
j
johnmcauley@gmail.com wrote:
I am a newbie and therefore I am currently discovering the delights of
Activerecord. I have a collection of objects, some of which have
children and some of which have not. I am trying to run a find which
will retrieve all parent objects which have children and then return
an array. I know there is some sleek way to achieve this, and not the
cumbersome iterative approach which looks to be looming ahead.
Parent.find :all, :select => 'unique parents.*',
:joins => 'inner join children on children.parent_id = parents.id'
Thanks for you reply,
I didn't need to use a join I just ran the SQL through
Parent.find_by_sql('SELECT parents.id, parents.name FROM parents,
children WHERE parents.id = parents.country_id')
I was hoping to use Active record instead of applying direct SQL but
thems the breaks.
Laters,
j
johnmcauley@gmail.com wrote:
I didn't need to use a join I just ran the SQL through
Parent.find_by_sql('SELECT parents.id, parents.name FROM parents,
children WHERE parents.id = parents.country_id')
The comma is just syntactic sugar for an inner join.