dbi: parent / child relationship

P

Paul Vudmaska

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
 
K

Kaspar Schiess

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFAfwQNFifl4CA0ImQRAuz6AJ0QtMP7lXnPh/YNyZGFGtFzN8H75wCfeDKi
SQ1lHAAHO1aG0hAvwIXNbyI=
=EUgV
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top