I got it working - attached is the code for it, as this may make more
sense than just my description.

But let me try to explain again anyway.

You have an ActiveRecord model with a polymorphic relationship, the
question is, how do you represent this in star schema?

The polymorphic relationship is represented as 2 columns - foo_id, foo_type.
For example: assume a row =
    {foo_id => 1, foo_type => 'User'}

Really, what I want to be able to do reporting and other good things
is to have a specific key to the user_dimension table, rather than
having to use both these columns as a key.  That is a fine rick for
ActiceRecord, but it's not exactly standard, and the class name
sitting in foo_type is pretty useless outside AR.

So that is what this row processor solves: it will add new a column
value to a row based on the polymorphic values.  Continuing the
example, after you run the processor on a row, you'll have the
following:
    {foo_id => 1, foo_type => 'User', :foo_user_id=>1}

Here it has added :foo_user_id => 1, which can now be used as a
foreign key to the user_dimension, and easily used with standard tools
and queries.

Is no one else using ActiveRecord models with polymorphic relationships?
Anyone who is - do you have a better solution? Or do you find this
denormalizing unnecessary.

Andrew Kuklewicz


On Wed, Apr 1, 2009 at 11:26 AM, Andrew Kuklewicz <kooks...@gmail.com> wrote:
> Hey folks,
>
> After a bit of a hiatus to deal with other things, I am back into my
> AW implementation.
>
> For one of the fact tables I want to create, it is based off an AR
> model's table that uses a polymorphic relationship, so I am thinking
> about how to handle this using AW ETL.
>
> I'm thinking it would be best to denormalize this with a row
> processor, and looking for advice on how best to do it.
>
> To use my app as an example, we have an activity feed (think facebook
> feed, yes, I know, not original) that has the general form:
> ":user does :action to :action_object"
>
> The :user part is easy, this will be stored in the fact table as an id
> relating to the user dimension.
>
> The :action is a string value, so I'll probably create a dimension for
> this with all possible values, so this too will end up a reference to
> a dimension.
> (probably use the foreign key transform for this, right?)
>
> The :action_object is polymorphic, with 2 columns, :action_object_id,
> and :action_object_type.
>
> The row processor would:
> First, get all possible values for 'action_object_type', e.g. 'Foo', and 
> 'Bar'.
> Map the type value to new columns per value, so 'foo_id' and 'bar_id'
> in this case will need to be added to each row.
> As each row is processed, if a row has the values
> action_object_type='Foo', and action_object_id=1,
> then the processor would add foo_id=1 and bar_id=nil.
> The assumption here is that there are corresponding dimensions for Foo
> and Bar to go with these new columns.
>
> Does this make sense?
> Has anyone else solved this a different way?
> Anyone interested in the polymorphic processor code once I finish it?
> (I am testing it now, but looking for validation before I move ahead
> using it)
>
> Cheers,
>
> Andrew Kuklewicz
>

Attachment: polymorphic_processor.rb
Description: Binary data

_______________________________________________
Activewarehouse-discuss mailing list
Activewarehouse-discuss@rubyforge.org
http://rubyforge.org/mailman/listinfo/activewarehouse-discuss

Reply via email to