I''m trying to migrate a PHP application to Rails, and I want to do
this right. I''ve got a data model that expresses a party''s
type,
their relationship, and the length of that relationship in several
tables. How much coercion will ActiveRecord need to make this work?
Is there a simpler (better) way of modeling these relationships? This
is what I have in the model:
parties: { id name }
party_relationships: { id party_id_from party_id_to from_date
thru_date party_role_type_id_from party_role_type_id_to }
party_role_types: { id name }
Let''s say we have a party called HSI, and a another called Knotty.
Knotty is a customer of HSI, but at the same time, in some
circumstances, HSI is also a customer of Knotty. In the application
I''m modeling, this happens a lot. This is what the data looks like
like:
parties:
{ 1 ''HSI'' }
{ 2 ''Knotty'' }
party_role_types:
{ 1 ''Customer'' }
{ 2 ''Vendor'' }
party_relationships (null means ongoing):
{ 1 2 1 ''2005-01-01'' ''2005-08-01'' 2 1 }
{ 2 1 2 ''2005-06-01'' null 1 2 }
Here''s the party_relationships table with real names instead of keys
for clarity:
{ 1 ''Knotty'' ''HSI''
''2005-01-01'' ''2005-08-01''
''Vendor'' ''Customer'' }
{ 2 ''HSI'' ''Knotty''
''2005-06-01'' null ''Customer''
''Vendor'' }
The date ranges are important, some of the reports that come out of
the app depend on them. So how do I make this work with ActiveRecord?
class Party < ActiveRecord::Base
has_and_belongs_to_many :party_relationships
# ...
end
class PartyRoleType
belongs_to_many :party_relationships
# ...
end
class PartyRelationships
has_and_belongs_to_many :parties
has_and_belongs_to_many :party_relationships
# ...
end
Did I get that right? Will this work well? How can I make all of this
better?
Thanks!
Beau