Skip to content

Instantly share code, notes, and snippets.

@stevecass
Created April 7, 2016 18:54
Show Gist options
  • Save stevecass/7a4d3f9f31f64c3b7fd3904dc6a1914d to your computer and use it in GitHub Desktop.
Save stevecass/7a4d3f9f31f64c3b7fd3904dc6a1914d to your computer and use it in GitHub Desktop.
# Model files
class Match < ActiveRecord::Base
belongs_to :source, foreign_key: 'from_id', class_name: Personality
belongs_to :target, foreign_key: 'to_id', class_name: Personality
end
class Person < ActiveRecord::Base
belongs_to :personality
def matches
Person.where(personality_id: personality.matching_personalities.pluck(:id))
end
end
class Personality < ActiveRecord::Base
has_many :matches, foreign_key: 'from_id'
has_many :matching_personalities, through: :matches, source: :target
end
# schema.rb
ActiveRecord::Schema.define(version: 20160407182307) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "matches", force: :cascade do |t|
t.integer "from_id"
t.integer "to_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "people", force: :cascade do |t|
t.integer "personality_id"
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "personalities", force: :cascade do |t|
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment