Dbi: parent / child relationship

I’ve got a few situations where i’m selecting a parent child
relationship with a join. I’m looking for an elegant way of iterating
through the children. Using the join, the parent table data is redundant
when there are muliple children.

for example, some pseudo code:
db.select_all(‘select * from parent,child where parent.id =
child.idParent’) .each do |r|
if this_parent_id = old_parent_id
#must be a child, or first parent
paint_child
else
paint_new_parent
end
end
#or this.
db.select_all(‘select id from parent’).each do |r|
paint_parent
db.select_all('select * from child where idParent = ?,r[‘id’]).each
do |r|
paint_parent
end
end

Neither of these is elegant or efficient. Does anyone have a more ruby
way of working with a db parent/child relationship?

:Paul

Using Kansas, back wherever I was setting up my database handle, I’d
define the relationship:

KSDatabase::Parent.to_many(:children, :Child, :idParent)

This says to create a relationship, ‘children’, from Parent to Child,
relating the primary key of Parent to the idParent field of child.

Then, to do the query and iterate:

ksdbh is the Kansas object.

ksdbh.select(:Parent).each do |parent|
paint_parent
parent.children do |child|
paint_child
end
end

Kirk Haines

···

On Fri, 16 Apr 2004, Paul Vudmaska wrote:

I’ve got a few situations where i’m selecting a parent child
relationship with a join. I’m looking for an elegant way of iterating
through the children. Using the join, the parent table data is redundant
when there are muliple children.

for example, some pseudo code:
db.select_all(‘select * from parent,child where parent.id =
child.idParent’) .each do |r|
if this_parent_id = old_parent_id
#must be a child, or first parent
paint_child
else
paint_new_parent
end
end
#or this.
db.select_all(‘select id from parent’).each do |r|
paint_parent
db.select_all('select * from child where idParent = ?,r[‘id’]).each
do |r|
paint_parent
end
end

Neither of these is elegant or efficient. Does anyone have a more ruby
way of working with a db parent/child relationship?

Paul Vudmaska wrote:

I’ve got a few situations where i’m selecting a parent child
relationship with a join. I’m looking for an elegant way of iterating
through the children. Using the join, the parent table data is redundant
when there are muliple children.

If I get you right there, you are talking of a
specialisation/generalisation relationship here. Lafcadio has a nice way
of handling that (lafcadio=another db abstraction layer): Inheritance in
~ Ruby.

You’d have a class ‘User’ for example with the attribute #login, and
then you would define a class ‘Customer’ to be
class Customer < User

end

Based on the table contents of the users table, you would get content
from both tables. Actually you’d have Customer Objects that have their
content from two (or more) locations.

Your loop becomes

customers = objectStore.getAll Customer
customers.each do |customer|
~ # access customer attributes as attributes of instance customer
end

assuming you want 'em all.

No more sql. And I find lafcadio to be reasonably fast for most of my
applications.

Hoping that I am not completely missing the point of your post…

kaspar - code philosopher

  • – stolen off the net –
    “I have been listening,” said Arthur, “but I’m not sure it’s helped.”
    ~ Ford grasped him by the lapels of his dressing gown and spoke to him
    ~ as slowly and distinctly and patiently as if he were somebody from a
    ~ telephone company accounts department.
    – Douglas Adams

Kirk Haines wrote:

···

On Fri, 16 Apr 2004, Paul Vudmaska wrote:

I’ve got a few situations where i’m selecting a parent child
relationship with a join. I’m looking for an elegant way of iterating
through the children. Using the join, the parent table data is redundant
when there are muliple children.

for example, some pseudo code:
db.select_all(‘select * from parent,child where parent.id =
child.idParent’) .each do |r|
if this_parent_id = old_parent_id
#must be a child, or first parent
paint_child
else
paint_new_parent
end
end
#or this.
db.select_all(‘select id from parent’).each do |r|
paint_parent
db.select_all('select * from child where idParent = ?,r[‘id’]).each
do |r|
paint_parent
end
end

Neither of these is elegant or efficient. Does anyone have a more ruby
way of working with a db parent/child relationship?

Using Kansas, back wherever I was setting up my database handle, I’d
define the relationship:

KSDatabase::Parent.to_many(:children, :Child, :idParent)

This says to create a relationship, ‘children’, from Parent to Child,
relating the primary key of Parent to the idParent field of child.

Then, to do the query and iterate:

ksdbh is the Kansas object.

ksdbh.select(:Parent).each do |parent|
paint_parent
parent.children do |child|
paint_child
end
end

Kirk Haines

That sure likes like a ruby way, i’ll look into Kansas, thank you.
:Paul

It’s very alpha right now. I mean, it works, but I’m adding a lot to it
very actively, and some stuff is shifting around a bit. I need to finish
some tests and then I will have a 0.0.2 version to release. Maybe still
tonight.

Thanks,

Kirk Haines

···

On Fri, 16 Apr 2004, Paul Vudmaska wrote:

That sure likes like a ruby way, i’ll look into Kansas, thank you.
:Paul