Skip to content

Instantly share code, notes, and snippets.

@beck03076
Last active August 29, 2015 14:21
Show Gist options
  • Save beck03076/dedb3d5069737df319fa to your computer and use it in GitHub Desktop.
Save beck03076/dedb3d5069737df319fa to your computer and use it in GitHub Desktop.
======= Emails =========
Enquiry, Registration, Partner, People has_and_belongs_to_many: emails
Email has_and_belongs_to_many: enquiries,registrations, partners, people
======= Others - Has Many =========
Enquiry, Registration, Partner, People has_many: tasks, as: :taskable
Enquiry, Registration, Partner, People has_many: follow_ups, as: :follow_upable
Enquiry, Registration, Partner, People has_many: notes, as: noteable
======= Others - Belongs To ==========
Task belongs_to: taskable, polymorphic: true
FollowUp belongs_to: follow_upable, polymorphic: true
Note belongs_to: noteable, polymorphic: true
@andyhite
Copy link

class Enquiry
  has_many :items, class_name: 'EnquiryItem'
  has_many :emails, :through => :enquiry_items
  has_many :tasks, :through => :enquiry_items
  has_many :follow_ups, :through => :enquiry_items
  has_many :notes, :through => :enquiry_items
end

class EnquiryItem
  belongs_to :enquiry
  has_many :emails, :as => :emailable
  has_many :tasks, :as => :taskable
  has_many :follow_ups, :as => :follow_upable
  has_many :notes, :as => :noteable
end

class Registration
  has_many :items, class_name: 'RegistrationItem'
  has_many :emails, :through => :registration_items
  has_many :tasks, :through => :registration_items
  has_many :follow_ups, :through => :registration_items
  has_many :notes, :through => :registration_items
end

class RegistrationItem
  belongs_to :registration
  has_many :emails, :as => :emailable
  has_many :tasks, :as => :taskable
  has_many :follow_ups, :as => :follow_upable
  has_many :notes, :as => :noteable
end

class Partner
  has_many :items, class_name: 'PartnerItem'
  has_many :emails, :through => :partner_items
  has_many :tasks, :through => :partner_items
  has_many :follow_ups, :through => :partner_items
  has_many :notes, :through => :partner_items
end

class PartnerItem
  belongs_to :partner
  has_many :emails, :as => :emailable
  has_many :tasks, :as => :taskable
  has_many :follow_ups, :as => :follow_upable
  has_many :notes, :as => :noteable
end

class Person
  has_many :items, class_name: 'PersonItem'
  has_many :emails, :through => :people_items
  has_many :tasks, :through => :people_items
  has_many :follow_ups, :through => :people_items
  has_many :notes, :through => :people_items
end

class PersonItem
  belongs_to :person
  has_many :emails, :as => :emailable
  has_many :tasks, :as => :taskable
  has_many :follow_ups, :as => :follow_upable
  has_many :notes, :as => :noteable
end

class Emails
  belongs_to :emailable, polymorphic: true
end

class Task
  belongs_to :taskable, polymorphic: true
end

class FollowUp
  belongs_to :follow_upable, polymorphic: true
end

class Note
  belongs_to :noteable, polymorphic: true
end

class Action
  def items(object) # where object is an Enquiry, Registration, Partner, or Person
    object.items.order('created_at DESC')
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment