dbi: parent / child relationship

Discussion in 'Ruby' started by Paul Vudmaska, Apr 15, 2004.

  1. 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
     
    Paul Vudmaska, Apr 15, 2004
    #1
    1. Advertising

  2. -----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-----
     
    Kaspar Schiess, Apr 15, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. davout
    Replies:
    0
    Views:
    553
    davout
    Apr 18, 2004
  2. Bob Day

    Parent/Child Relationship

    Bob Day, Nov 5, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    142
    Jacob Yang [MSFT]
    Nov 6, 2003
  3. Grey

    Check Parent-child relationship in datagrid

    Grey, Mar 18, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    233
    Elton Wang
    Mar 18, 2005
  4. Noel Dolan
    Replies:
    0
    Views:
    238
    Noel Dolan
    Jul 18, 2004
  5. Bitswapper
    Replies:
    5
    Views:
    151
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page