0

I am trying to associate Contacts with Classes but as two different types. Current_classes and Interested_classes.

I know I need to enable polymorphic but I am not sure as to where it needs to be enabled.

This is what I have at the moment

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

And then inside of my Contacts Model I want to have something like this.

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

What am I doing wrong?

Josh Crowder
  • 1,041
  • 1
  • 10
  • 30

1 Answers1

0

I can give u the answer but better you read this post Polymorphic Associations from rails guide

Mohit Jain
  • 43,139
  • 57
  • 169
  • 274
  • I've read through that but it hasn't helped with my problem. Do I need to create another field in the join table which links to current or interested? – Josh Crowder May 24 '10 at 13:11
  • First of all u are not following any convention. Try to follow it. Its really helpful.DO it again using proper convention. Seriously i m not giving u answer cause somebody helped me in the same way.Read that article i mentioned again from scratch – Mohit Jain May 25 '10 at 09:53
  • Thanks for the advise, I re-read though it and found a much better way of doing it! – Josh Crowder Jun 11 '10 at 16:38