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 

我在做什麼錯?

回答

0

我可以給ü的答案,但你更好地閱讀這篇文章Polymorphic Associations from rails guide

+0

我通過閱讀,但它並沒有與我的問題有幫助。我是否需要在鏈接到當前或感興趣的連接表中創建另一個字段? – 2010-05-24 13:11:24

+0

首先你不遵循任何約定。嘗試遵循它。它真的很有用。它再次使用適當的約定。說真的,我沒有給你答案因爲有人幫助我以同樣的方式。閱讀文章,我再次從頭提到 – 2010-05-25 09:53:57

+0

感謝您的建議,我重新閱讀,但它發現了一個更好的方式做到這一點! – 2010-06-11 16:38:04