Best way of doing this structure (legacy db), STI? Polymorphism?

I’m working with a legacy Oracle DB (read-only access) and am
wondering if I’m going down the correct path with using STI.
Basically, there is one huge table (167+ fields) which contain
configuration information for about 8 different things. Some fields
are applicable to only certain types of things and other fields are
applicable to all things. Obviously, fields that aren’t applicable
have nill values. Also, the type of thing is conveniently stored as a
type field. The key field for this table is l_name.

There is another table which shows relationships among the things. It
does this by using both l_name and r_name fields. Say you have a
thing with l_name=‘glass’. When you query the relationship table with
r_name => ‘glass’ then you get the downstream dependents of ‘glass’.
Conversely, if you query the relationship table with l_name=‘glass’
then you get the upstream dependencies.

I want to be able to walk this tree either up or down from any given
point as well as get all of the upstream/downstream dependencies from
any given point.

What I’ve done so far is to use STI and have sub classed each type of
thing into it’s own model. I’ve also created a model for the
relationships and have sub classed that model into upstream_dependents
and downstream_dependents.

class UpstreamDependents
has_many room_types, :foreign_key => :l_name
has_many :things, :foreign_key => :l_name
end

class DownstreamDependents
has_many :walls, :foreign_key => :r_name
has_many :fans, :foreign_key => :r_name
has_many :things, :foreign_key => :r_name
end

class Thing
has_many :upstream_dependents, :foreign_key => :l_name
has_many :downstream_dependents, :foreign_key => :r_name
end

Now in each of my individual models, I can do something like:
class Room < Thing
has_many :walls, :through => :downstream_relationships
has_many :fans, :through => :downstream_relationships
has_many :room_types, :through => :upstream_relationships
end

So, this seems to be working and I can do what I want to so far.
However, this seems a bit kludgy. Is what I’m doing the way to go on
this? Is there a better way?

One thing that would be nice, is to be able to do something like:
test = Thing.find(4)
items = test.downstream_dependents

And have items actually be a a list of the Room/Wall/Fans/RoomTypes

Thanks in advance!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs