Ruby on rails 通过关系类为2个以上的类创建多对多关联

Ruby on rails 通过关系类为2个以上的类创建多对多关联,ruby-on-rails,ruby,ruby-on-rails-3,class,associations,Ruby On Rails,Ruby,Ruby On Rails 3,Class,Associations,我正在使用RubyonRails3,我想通过关系类为2个类中的更多类设置多对多关联 例如,我有: class RelationshipGroup < ActiveRecord::Base # This is the relationship class # Maybe the Schema Information should be the following: # # Table name: RelationshipGroup # # id : integer

我正在使用RubyonRails3,我想通过关系类为2个类中的更多类设置多对多关联

例如,我有:

class RelationshipGroup < ActiveRecord::Base # This is the relationship class

  # Maybe the Schema Information should be the following:
  #
  # Table name: RelationshipGroup
  #
  # id    : integer
  # dog_id: integer
  # cat_id: integer
  # cow_id: integer

  ...
end 

class Dog < ActiveRecord::Base
  ...
end 

class Cat < ActiveRecord::Base
  ...
end 

class Cow < ActiveRecord::Base
  ...
end 
class RelationshipGroup

在上面的示例中,我想设置这些关联,以便可以使用
RelationshipGroup
类进行搜索,从而可以检索属于某个组的所有动物。我知道如何使用
有很多:通过
关联两个类,但不适用于更多的2个类。所以,有可能做到这一点(也许我必须使用关联扩展或类方法来实现这一点?)?如果是这样,我必须如何为上面的示例编写代码?

您可以通过连接表使用多态关联

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 
class RelationshipGroup:会员资格
结束
阶级成员
#字段-关系\组\ id、成员\ id、成员\类型
属于:关系组
属于:成员,:多态=>true
结束
类Dog:会员
拥有多个:关系组,:至=>:成员身份
结束
类Cat:会员
拥有多个:关系组,:至=>:成员身份
结束
类Cow:会员
拥有多个:关系组,:至=>:成员身份
结束
更进一步说,最好将关联(都是相同的)移动到一个模块中,使其干燥:

#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 
lib/member.rb中的
#
模块成员
def自带(基本)
基本类\u评估do
拥有多个:会员资格,:as=>:会员
拥有多个:关系组,:至=>:成员身份
结束
结束
结束
类RelationshipGroup:会员资格
结束
阶级成员
#字段-关系\组\ id、成员\ id、成员\类型
属于:关系组
属于:成员,:多态=>true
结束
类Dog
有一种方法可以避免创建“成员”类,可以在“RelationshipGroup”类中添加“RelationshipGroup\u id”列,并使用该字段进行关联?如果是,我如何编写代码?在我的实际案例中,“RelationshipGroup”类已经包含“dog\u id”、“cat\u id”、“cow\u id”列。