I'm running into a number of cases in my current project where it
would be natural for a model's association data to be presented not as
an array but as an hash keyed by an attribute of the associated model,
typically a column from its composite primary key. For example:

class Player < Sequel::Model
   one_to_many :game_data
end

class PlayerGameDatum < Sequel::Model
  many_to_one :player
  many_to_one :game
  # PK might just be player_id, game_id, or might have additional
columns (e.g. datatype_id)
end

It's common to want to look up the data for a particular game given a
player. The default way to do this in sequel would be:

class Player < Sequel::Model
  def data(game)
    game_data.find_all{|datum| datum.game_id == game.id}
    # or game_data_dataset.filter(:game_id => game.id).all
  end
end

But that does either a linear search or a database query. I'd rather
be indexing into a hash. My solution thus far is:

  def data(game)
    @data ||= game_data.group_by {|datum| datum.game_id}
    @data[game.id]
  end

But now I'm storing the data in two different datastructures, and
really only using one of them. Plus I end up repeating similar code in
the Game class, to look up data by player.

Has anyone else dealt with this problem? What was your solution? Would
it make a good plugin or extension?

John

--~--~---------~--~----~------------~-------~--~----~
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