0

我试图将联系人与类相关联,但作为两种不同的类型。Current_classes 和 Interested_classes。

我知道我需要启用多态,但我不确定它需要在哪里启用。

这就是我目前所拥有的

class CreateClasses < ActiveRecord::Migration
  def self.up
    create_table :classes do |t|
      t.string :class_type
      t.string :class_name
      t.string :date

      t.timestamps
    end
  end

  def self.down
    drop_table :classes
  end
end

class CreateContactsInterestedClassesJoin < ActiveRecord::Migration
  def self.up
    create_table 'contacts_interested_classes', :id => false do |t|
      t.column 'class_id', :integer
      t.column 'contact_id', :integer
    end
  end

  def self.down
    drop_table 'contacts_interested_classes'
  end
end

class CreateContactsCurrentClassesJoin < ActiveRecord::Migration
  def self.up
    create_table 'contacts_current_classes', :id => false do |t|
      t.column 'class_id', :integer
      t.column 'contact_id', :integer
    end
  end

  def self.down
    drop_table 'contacts_current_classes'
  end
end

然后在我的联系人模型中,我想要这样的东西。

class Contact < ActiveRecord::Base
  has_and_belongs_to_many :classes, :join_table => "contacts_interested_classes", :foreign_key => "class_id" :as => 'interested_classes'
  has_and_belongs_to_many :classes, :join_table => "contacts_current_classes", :foreign_key => "class_id" :as => 'current_classes'
end

我究竟做错了什么?

4

1 回答 1

0

我可以给你答案,但你最好从 Rails 指南中阅读这篇文章 Polymorphic Associations

于 2010-05-24T10:29:01.820 回答