On Dec 20, 3:43 pm, Yorick Peterse <[email protected]> wrote:
> Fellow geeks,
>
> I'm a geek, just like you, and I use Sequel, just like you (why else would I
> be here? ;)). And just like you I have a problem (no, not that kind of
> problem).
> Now what do geeks do when they have a problem? They Google? Maybe
> Stackoverflow? Nope, they write silly Emails and smack them on a mailing
> list so other folks can solve the issue.
>
> Anyway, enough of that. What I'm trying to achieve is probably quite simple
> and can be achieved by writing a custom method. However, before doing so I'd
> like to know if it's not something
> available by default. The problem is that I have 2 tables, "old men" and
> "little kids". The relation system is set up so that an old man can have
> many little kids and a little kid can belong to many old men.
> Whenever using a web UI users can assign which kids belong to what old man,
> but here's the problem. As this association is a many to many it means we'll
> have to deal with a join table and can't update
> the related item(s) by just changing a column in the "old men" table.
>
> The common way is to create N instances of a little kid and pass them to the
> add_little_kid or remove_little_kid method. What I dislike about this is
> that first we have to send N queries to retrieve the required
> records from the "little kids" table and then have to pass these objects to
> a method which in turn will most likely be placed in an if statement (to
> determine if the row is new or not). This would result in the following
> pseudo code:
>
> little_kids = post["little_kid_ids"].map do |id|
>
>   LittleKid[id]
>
> end # => [#<Object 1>, #<Object 2>]
>
> > OldMan.add_little_kids(little_kids)

This is broken, you'd have to do:

  post["little_kid_ids"].each do |id|
    OldMan.add_little_kid(LittleKid[id])
  end

with the master branch, you can do:

  post["little_kid_ids"].each do |id|
    OldMan.add_little_kid(id)
  end

But that's not any better performing as it still does a query to get
the LittleKid.

> While this code is greatly simplified (and probably broken) you most likely
> get the idea: way too many queries and loops involved for something
> relatively simple. Jeremy Evans from the #sequel channel mentioned the
> "association_pks" plugin
> so I'll take a look at that tomorrow but I'd like to know if there are other
> possible ways of doing it.

I'm pretty sure it's what you want. Here's a link to the
documentation: 
http://sequel.rubyforge.org/rdoc-plugins/classes/Sequel/Plugins/AssociationPks.html

Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to