On Oct 19, 10:56 am, Nate Wiger <[email protected]> wrote:
> Sorry for the delayed response - those changes all look cool, dig it.
>
> I did have one question, though - won't this section in
> remove_associated_object force another DB lookup if you pass in an
> object?
>
> +          elsif send(opts.dataset_method).filter(o.pk_hash).empty?
> +            raise(Sequel::Error, "associated object #{o.inspect} is
> not currently associated to #{inspect}")
> +          end

Yes it will.

> So doing:
>
>    player = Player[1]
>    game = Game[2]
>    game.remove_player(player)
>
> Would result in two fetches?
>
>    select * from players where id = 1
>    select * from players where game_id = 2 and id = 1  # to check
> assoc

Actually, three fetches, don't forget about Game[2]

> If so (assuming there's not caching I'm unaware of), is there a way to
> do that without the fetch?  Is there a member method that would return
> the FK?
>
> +          elsif send(o.fk_hash_for(self)) != pk
> +            raise(Sequel::Error, "associated object #{o.inspect} is
> not currently associated to #{inspect}")
> +          end
>
> (Obviously that's non-working code but hopefully the idea is clear)

While that is more optimized, it's problematic due to type conversion
issues (passing in a string when the value is an integer or vice
versa).  Going out to the database to check is more likely to work in
all circumstances, and Sequel values reliability over performance.
You can always just pass in the primary key and have the lookup be
scoped to the association, so only a single query is used:

  game = Game[2]
  game.remove_player(1)

I'm not opposed to changing the implementation, so send in a patch if
you think it should change and want to deal with the type conversion
issues (including dealing with composite keys).  You will probably
want to refactor the check into its own method so it's easy to
override.

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