On Oct 20, 8:26 am, Nate Wiger <[email protected]> wrote:
> Now that Jeremy's pushed the new ID-based add/remove association
> helpers (thanks!) I'm having trouble with a noobie question.
>
> How do I do a find by primary key thru the association?  In AR that
> would be:
>
>     game = Game[5]
>     game.game_players.find(17)
>
> I tried
>
>    game.find_game_player(17)
>    game.first_game_player(17)
>    game.get_game_player(17)
>
> But no luck.

Your assumption that there are a bunch of magic methods created leads
me to believe that you haven't read the documentation:
http://sequel.rubyforge.org/rdoc/classes/Sequel/Model/Associations/ClassMethods.html

When you do "Game.one_to_many :game_players", Sequel is going to
create only the following 5 methods:

* game_players - Array of all associated game players
* add_game_player, remove_game_player, and remove_all_game_players -
You probably know about these
* game_players_dataset - Dataset representing associated game players

No other methods are created.  Virtually any action you'd want that
isn't handled via the main association method or the add/remove/
remove_all methods can probably be accomplished using the dataset
method.  Unlike AR, this isn't a funky proxy class, an association
dataset acts just like any standard Sequel dataset.  That means that
pretty much anything you can do to a standard dataset, you can do to
an association dataset.

Phrogz answer is correct, but it's important you understand why.

In a normal dataset, if you want to return the first row that with a
column that matches a value:

  dataset[:column=>value]

So to find the first/only row with an :id (primary key) column that
has the value 17:

  dataset[:id=>17]

Since you want the dataset to only be comprised of players for a
specific game:

  game.game_players_dataset[:id=>17]

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