Skip to content

Instantly share code, notes, and snippets.

@wolfieorama
Last active April 1, 2016 08:08
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 wolfieorama/3dbf73ab3fe9274ce4779b6abd89b070 to your computer and use it in GitHub Desktop.
Save wolfieorama/3dbf73ab3fe9274ce4779b6abd89b070 to your computer and use it in GitHub Desktop.
class Employee < ActiveRecord::Base
has_many :subordinates, class_name: "Employee", foreign_key: "manager_id"
belongs_to :manager, class_name: "Employee"
belongs_to :team
has_one :team, foreign_key: "manager_id"
end
create_table "employees", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "manager_id"
t.integer "team_id"
end
create_table "teams", force: :cascade do |t|
t.string "team_name"
t.integer "sec_team_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "ter_team_id"
t.integer "manager_id"
end
add_index "teams", ["sec_team_id"], name: "index_teams_on_sec_team_id"
end
class Team < ActiveRecord::Base
#self join within teams
has_one :teir1, class_name: "Team", foreign_key: "sec_team_id"
has_one :teir2, class_name: "Team", foreign_key: "ter_team_id"
belongs_to :sec_team, class_name: "Team"
belongs_to :ter_team, class_name: "Team"
#across users
has_many :subordinates, class_name: "Employee", foreign_key: "subordinate_id"
belongs_to :manager, class_name: "Employee"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment