Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > dbi: parent / child relationship

Reply
Thread Tools

dbi: parent / child relationship

 
 
Paul Vudmaska
Guest
Posts: n/a
 
      04-15-2004
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?

aul


 
Reply With Quote
 
 
 
 
Kaspar Schiess
Guest
Posts: n/a
 
      04-15-2004
-----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-----


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
Check Parent-child relationship in datagrid Grey ASP .Net Datagrid Control 1 03-18-2005 07:43 PM
Pass from parent to child, then update parent with child value... Noel Dolan Javascript 0 07-18-2004 05:52 PM
EJB: How to delete child entries from a parent-child CMR one-to-many relationship? davout Java 0 04-18-2004 07:02 PM
Parent/Child Relationship Bob Day ASP .Net Datagrid Control 1 11-06-2003 01:13 AM



Advertisments