Skip to content

Instantly share code, notes, and snippets.

@aaronvb
Created November 30, 2012 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronvb/4173290 to your computer and use it in GitHub Desktop.
Save aaronvb/4173290 to your computer and use it in GitHub Desktop.
class Checkpoint < ActiveRecord::Base
has_many :note_joins, as: :notable
has_many :notes, through: :note_joins
end
class CreateNoteJoins < ActiveRecord::Migration
def change
create_table :note_joins do |t|
t.integer :note_id
t.integer :notable_id
t.string :notable_type
t.timestamps
end
add_index :note_joins, [:notable_id, :notable_type]
end
end
class CreateNotes < ActiveRecord::Migration
def change
create_table :notes do |t|
t.text :content
t.integer :user_id
t.timestamps
end
end
end
class Location < ActiveRecord::Base
has_many :note_joins, as: :notable
has_many :notes, through: :note_joins
end
class Note < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :notable, polymorphic: true
belongs_to :user
has_many :note_joins
end
class NoteJoin < ActiveRecord::Base
belongs_to :notable, polymorphic: true
belongs_to :note
end
class Note < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :notable, polymorphic: true
belongs_to :user
has_many :note_joins
has_many :locations, through: :note_joins, source: :notable, source_type: 'Location'
has_many :checkpoints, through: :note_joins, source: :notable, source_type: 'Checkpoint'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment