On Saturday, May 3, 2014 3:02:06 PM UTC-7, Sean West wrote:
>
> I'm writing sports statistics software using Sequel which will handle 
> multiple sports. With each sport comes slight variations on the database 
> schema, especially with the statistics. I'm trying to find the best way to 
> accommodate these differences seamlessly.
>
> As an example, let's say I have the following structure:
>
> DB.create_table(:leagues) do
>     primary_key :id
>     String :sport
> end
>
> DB.create_table(:teams) do
>     primary_key :id
>     foreign_key :league_id, :leagues, :key => [:id]
> end
>
> DB.create_table(:team_stats_hockey) do
>     foreign_key :id, :teams, :key => [:id] 
>
>     # hockey related stats column 
>
> end
>
> DB.create_table(:team_stats_football) do
>     foreign_key :id, :teams, :key => [:id] 
>
>     # football related stats column 
>
> end
>
>
> I modelled this so that it would work with the class table inheritance 
> plugin, however I would like a call such as Team.first to return the 
> appropriate stats object vs. just the team object. Since the teams table 
> does not have the sport column available, I can't just set the plugin's key 
> setting.
>

Depending on how you configure the plugin, you can get Team.first to return 
a TeamStatsHockey instance, but you have to have a field in teams that 
indicates the type of team.  But based on your schema, that's not possible, 
and may not be a wise idea even if you want to change the schema.
 

> Is there any way I can use this plugin, but use the league's sport field 
> as the key? Or should I look into writing my own plugin to handle something 
> like this?
>

The class_table_inheritance plugin doesn't support that, you'll need to 
write your own.  However, based on what you've shown, I don't think trying 
to model this via class table inheritance is a good idea.  Here's a couple 
of options:

1) You could use the single_table_inheritance plugin with a model_map based 
on the league_id, with HockeyTeam and FootballTeam subclasses, and define 
an appropriate stats associations in HockeyTeam and FootballTeam to look at 
the appropriate stats table.

2) You could have completely separate tables for HockeyTeam and 
FootballTeam.  The only time this would cause you problems is if you have a 
lot of queries that need to consider teams from multiple sports. Note that 
if you do this, it's still easy to have all of the *Team classes share 
behavior, either via inheriting from an abstract base class or having them 
all include the same module or use the same plugin.

3) If you are using PostgreSQL, you can have a single teams table with an 
json stats column.  This is probably the simplest approach.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to