This looks like a good application for a polymorphic association:
class User < ActiveRecord::Base
has_one :contact, :as => :contactable
end
class Customer < ActiveRecord::Base
has_many :contacts, :as => :contactable
end
class Producer < ActiveRecord::Base
has_many :contacts, :as => :contactable
end
class Contact < ActiveRecord::Base
belongs_to :contactable, :polymorphic => true
end
EDIT
It seems I didn't read the specs all the way through :) To associate the same contact with multiple Users, Customers, etc, you could use a has_many :through
:
class User < ActiveRecord::Base
has_one :user_contact, :dependent => :destroy
has_one :contact, :through => :user_contact
end
class Customer < ActiveRecord::Base
has_many :customer_contacts, :dependent => :destroy
has_many :contacts, :through => :customer_contacts
end
class Producer < ActiveRecord::Base
has_many :producer_contacts, :dependent => :destroy
has_many :contacts, :through => :producer_contacts
end
class UserContact
belongs_to :user
belongs_to :contact
end
class CustomerContact
belongs_to :customer
belongs_to :contact
end
class ProducerContact
belongs_to :producer
belongs_to :contact
end
class Contact < ActiveRecord::Base
has_many :user_contacts, :dependent => :destroy # might use 'has_one' here depending on your requirements
has_many :users, :through => :user_contacts
has_many :customer_contacts, :dependent => :destroy
has_many :customers, :through => :customer_contacts
has_many :producer_contacts, :dependent => :destroy
has_many :producers, :through => :producer_contacts
end
That gives you one join table for each of the three associations. Each Contact can belong to none, one, or many of the three other models by adding rows to the join tables.