Вы можете использовать полиморфную ассоциацию через таблицу соединений.
class RelationshipGroup < ActiveRecord::Base # This is the relationship class
has_many :memberships
has_many :members, :through => :memberships
end
class Membership
#fields - relationship_group_id, member_id, member_type
belongs_to :relationship_group
belongs_to :member, :polymorphic => true
end
class Dog < ActiveRecord::Base
has_many :memberships, :as => :member
has_many :relationship_groups, :through => :memberships
end
class Cat < ActiveRecord::Base
has_many :memberships, :as => :member
has_many :relationship_groups, :through => :memberships
end
class Cow < ActiveRecord::Base
has_many :memberships, :as => :member
has_many :relationship_groups, :through => :memberships
end
Чтобы пойти немного дальше, было бы хорошо высушить это, перенеся ассоциации (которые все идентичны) в модуль:
#in lib/member.rb
module Member
def self.included(base)
base.class_eval do
has_many :memberships, :as => :member
has_many :relationship_groups, :through => :memberships
end
end
end
class RelationshipGroup < ActiveRecord::Base # This is the relationship class
has_many :memberships
has_many :members, :through => :memberships
end
class Membership
#fields - relationship_group_id, member_id, member_type
belongs_to :relationship_group
belongs_to :member, :polymorphic => true
end
class Dog < ActiveRecord::Base
include Member
end
class Cat < ActiveRecord::Base
include Member
end
class Cow < ActiveRecord::Base
include Member
end